source: notebooks/index.clj

Dataset (data frame) manipulation API for the tech.ml.dataset library

GenerateMe

(def tech-ml-version (get-in (read-string (slurp "deps.edn")) [:deps 'techascent/tech.ml.dataset :mvn/version]))
(def tablecloth-version (nth (read-string (slurp "project.clj")) 2))
tech-ml-version
"7.000-beta-51"
tablecloth-version
"7.000-beta-51"

Introduction

tech.ml.dataset is a great and fast library which brings columnar dataset to the Clojure. Chris Nuernberger has been working on this library for last year as a part of bigger tech.ml stack.

I’ve started to test the library and help to fix uncovered bugs. My main goal was to compare functionalities with the other standards from other platforms. I focused on R solutions: dplyr, tidyr and data.table.

During conversions of the examples I’ve come up how to reorganized existing tech.ml.dataset functions into simple to use API. The main goals were:

  • Focus on dataset manipulation functionality, leaving other parts of tech.ml like pipelines, datatypes, readers, ML, etc.
  • Single entry point for common operations - one function dispatching on given arguments.
  • group-by results with special kind of dataset - a dataset containing subsets created after grouping as a column.
  • Most operations recognize regular dataset and grouped dataset and process data accordingly.
  • One function form to enable thread-first on dataset.

If you want to know more about tech.ml.dataset and dtype-next please refer their documentation:

SOURCE CODE

Join the discussion on Zulip

Let’s require main namespace and define dataset used in most examples:

(require '[tablecloth.api :as tc]
         '[tech.v3.datatype.functional :as dfn])
nil
(def DS (tc/dataset {:V1 (take 9 (cycle [1 2]))
                     :V2 (range 1 10)
                     :V3 (take 9 (cycle [0.5 1.0 1.5]))
                     :V4 (take 9 (cycle ["A" "B" "C"]))}))
DS
loading …

Functionality

Dataset

Dataset is a special type which can be considered as a map of columns implemented around tech.ml.datatype library. Each column can be considered as named sequence of typed data. Supported types include integers, floats, string, boolean, date/time, objects etc.

Dataset creation

Dataset can be created from various of types of Clojure structures and files:

  • single values
  • sequence of maps
  • map of sequences or values
  • sequence of columns (taken from other dataset or created manually)
  • sequence of pairs: [string column-data] or [keyword column-data]
  • array of any arrays
  • file types: raw/gzipped csv/tsv, json, xls(x) taken from local file system or URL
  • input stream

tc/dataset accepts:

  • data
  • options (see documentation of tech.ml.dataset/->dataset function for full list):
    • :dataset-name - name of the dataset
    • :num-rows - number of rows to read from file
    • :header-row? - indication if first row in file is a header
    • :key-fn - function applied to column names (eg. keyword, to convert column names to keywords)
    • :separator - column separator
    • :single-value-column-name - name of the column when single value is provided
    • :column-names - in case you want to name columns - only works for sequential input (arrays) or empty dataset
    • :layout - for numerical, native array of arrays - treat entries :as-rows or :as-columns (default)

tc/let-dataset accepts bindings symbol-column-data to simulate R’s tibble function. Each binding is converted into a column. You can refer previous columns to in further bindings (as in let).


Empty dataset.

(tc/dataset)
loading …

Empty dataset with column names

(tc/dataset nil {:column-names [:a :b]})
loading …

Sequence of pairs (first = column name, second = value(s)).

(tc/dataset [[:A 33] [:B 5] [:C :a]])
loading …

Not sequential values are repeated row-count number of times.

(tc/dataset [[:A [1 2 3 4 5 6]] [:B "X"] [:C :a]])
loading …

Dataset created from map (keys = column names, vals = value(s)). Works the same as sequence of pairs.

(tc/dataset {:A 33})
loading …
(tc/dataset {:A [1 2 3]})
loading …
(tc/dataset {:A [3 4 5] :B "X"})
loading …

You can put any value inside a column

(tc/dataset {:A [[3 4 5] [:a :b]] :B "X"})
loading …

Sequence of maps

(tc/dataset [{:a 1 :b 3} {:b 2 :a 99}])
loading …
(tc/dataset [{:a 1 :b [1 2 3]} {:a 2 :b [3 4]}])
loading …

Missing values are marked by nil

(tc/dataset [{:a nil :b 1} {:a 3 :b 4} {:a 11}])
loading …

Reading from arrays, by default :as-rows

(-> (map int-array [[1 2] [3 4] [5 6]])
    (into-array)
    (tc/dataset))
loading …

:as-columns

(-> (map int-array [[1 2] [3 4] [5 6]])
    (into-array)
    (tc/dataset {:layout :as-columns}))
loading …

:as-rows with names

(-> (map int-array [[1 2] [3 4] [5 6]])
    (into-array)
    (tc/dataset {:layout :as-rows
                 :column-names [:a :b]}))
loading …

Any objects

(-> (map to-array [[:a :z] ["ee" "ww"] [9 10]])
    (into-array)
    (tc/dataset {:column-names [:a :b :c]
                 :layout :as-columns}))
loading …

Create dataset using macro let-dataset to simulate R tibble function. Each binding is converted into a column.

(tc/let-dataset [x (range 1 6)
                  y 1
                  z (dfn/+ x y)])
loading …

Import CSV file

(tc/dataset "data/family.csv")
loading …

Import from URL

(defonce ds (tc/dataset "https://vega.github.io/vega-lite/examples/data/seattle-weather.csv"))
nil
ds
loading …

When none of above works, singleton dataset is created. Along with the error message from the exception thrown by tech.ml.dataset

(tc/dataset 999)
loading …

To see the stack trace, turn it on by setting :stack-trace? to true.


Set column name for single value. Also set the dataset name and turn off creating error message column.

(tc/dataset 999 {:single-value-column-name "my-single-value"
                 :error-column? false})
loading …
(tc/dataset 999 {:single-value-column-name ""
                 :dataset-name "Single value"
                 :error-column? false})
loading …

Saving

Export dataset to a file or output stream can be done by calling tc/write!. Function accepts:

  • dataset
  • file name with one of the extensions: .csv, .tsv, .csv.gz and .tsv.gz or output stream
  • options:
  • :separator - string or separator char.
(tc/write! ds "output.tsv.gz")
nil
(.exists (clojure.java.io/file "output.tsv.gz"))
true
Nippy
(tc/write! DS "output.nippy.gz")
nil
(tc/dataset "output.nippy.gz")
loading …

Columns and rows

Get columns and rows as sequences. column, columns and rows treat grouped dataset as regular one. See Groups to read more about grouped datasets.

Possible result types:

  • :as-seq or :as-seqs - sequence of seqences (default)
  • :as-maps - sequence of maps (rows)
  • :as-map - map of sequences (columns)
  • :as-double-arrays - array of double arrays
  • :as-vecs - sequence of vectors (rows)

Select column.

(ds "wind")
#tech.v3.dataset.column<float64>[1461]
wind
[4.700, 4.500, 2.300, 4.700, 6.100, 2.200, 2.300, 2.000, 3.400, 3.400, 5.100, 1.900, 1.300, 5.300, 3.200, 5.000, 5.600, 5.000, 1.600, 2.300...]
(tc/column ds "date")
#tech.v3.dataset.column<packed-local-date>[1461]
date
[2012-01-01, 2012-01-02, 2012-01-03, 2012-01-04, 2012-01-05, 2012-01-06, 2012-01-07, 2012-01-08, 2012-01-09, 2012-01-10, 2012-01-11, 2012-01-12, 2012-01-13, 2012-01-14, 2012-01-15, 2012-01-16, 2012-01-17, 2012-01-18, 2012-01-19, 2012-01-20...]

Columns as sequence

(take 2 (tc/columns ds))
(#tech.v3.dataset.column<packed-local-date>[1461]
date
[2012-01-01, 2012-01-02, 2012-01-03, 2012-01-04, 2012-01-05, 2012-01-06, 2012-01-07, 2012-01-08, 2012-01-09, 2012-01-10, 2012-01-11, 2012-01-12, 2012-01-13, 2012-01-14, 2012-01-15, 2012-01-16, 2012-01-17, 2012-01-18, 2012-01-19, 2012-01-20...]
 #tech.v3.dataset.column<float64>[1461]
precipitation
[0.000, 10.90, 0.8000, 20.30, 1.300, 2.500, 0.000, 0.000, 4.300, 1.000, 0.000, 0.000, 0.000, 4.100, 5.300, 2.500, 8.100, 19.80, 15.20, 13.50...])

Columns as map

(keys (tc/columns ds :as-map))
("date" "precipitation" "temp_max" "temp_min" "wind" "weather")

Rows as sequence of sequences

(take 2 (tc/rows ds))
([#object[java.time.LocalDate 0x6bf062c3 "2012-01-01"]
  0.0
  12.8
  5.0
  4.7
  "drizzle"]
 [#object[java.time.LocalDate 0x79dfc1ac "2012-01-02"]
  10.9
  10.6
  2.8
  4.5
  "rain"])

Select rows/columns as double-double-array

(-> ds
    (tc/select-columns :type/numerical)
    (tc/head)
    (tc/rows :as-double-arrays))
[[0.0, 12.8, 5.0, 4.7], [10.9, 10.6, 2.8, 4.5], [0.8, 11.7, 7.2, 2.3],
 [20.3, 12.2, 5.6, 4.7], [1.3, 8.9, 2.8, 6.1]]
(-> ds
    (tc/select-columns :type/numerical)
    (tc/head)
    (tc/columns :as-double-arrays))
[[0.0, 10.9, 0.8, 20.3, 1.3], [12.8, 10.6, 11.7, 12.2, 8.9],
 [5.0, 2.8, 7.2, 5.6, 2.8], [4.7, 4.5, 2.3, 4.7, 6.1]]

Rows as sequence of maps

(clojure.pprint/pprint (take 2 (tc/rows ds :as-maps)))
nil

Single entry

Get single value from the table using get-in from Clojure API or get-entry. First argument is column name, second is row number.

(get-in ds ["wind" 2])
2.3
(tc/get-entry ds "wind" 2)
2.3

Printing

Dataset is printed using dataset->str or print-dataset functions. Options are the same as in tech.ml.dataset/dataset-data->str. Most important is :print-line-policy which can be one of the: :single, :repl or :markdown.

(tc/print-dataset (tc/group-by DS :V1) {:print-line-policy :markdown})
nil
(tc/print-dataset (tc/group-by DS :V1) {:print-line-policy :repl})
nil
(tc/print-dataset (tc/group-by DS :V1) {:print-line-policy :single})
nil

Group-by

Grouping by is an operation which splits dataset into subdatasets and pack it into new special type of… dataset. I distinguish two types of dataset: regular dataset and grouped dataset. The latter is the result of grouping.

Grouped dataset is annotated in by :grouped? meta tag and consist following columns:

  • :name - group name or structure
  • :group-id - integer assigned to the group
  • :data - groups as datasets

Almost all functions recognize type of the dataset (grouped or not) and operate accordingly.

You can’t apply reshaping or join/concat functions on grouped datasets.

Grouping

Grouping is done by calling group-by function with arguments:

  • ds - dataset
  • grouping-selector - what to use for grouping
  • options:
    • :result-type - what to return:
      • :as-dataset (default) - return grouped dataset
      • :as-indexes - return rows ids (row number from original dataset)
      • :as-map - return map with group names as keys and subdataset as values
      • :as-seq - return sequens of subdatasets
    • :select-keys - list of the columns passed to a grouping selector function

All subdatasets (groups) have set name as the group name, additionally group-id is in meta.

Grouping can be done by:

  • single column name
  • seq of column names
  • map of keys (group names) and row indexes
  • value returned by function taking row as map (limited to :select-keys)

Note: currently dataset inside dataset is printed recursively so it renders poorly from markdown. So I will use :as-seq result type to show just group names and groups.


List of columns in grouped dataset

(-> DS
    (tc/group-by :V1)
    (tc/column-names))
(:V1 :V2 :V3 :V4)

List of columns in grouped dataset treated as regular dataset

(-> DS
    (tc/group-by :V1)
    (tc/as-regular-dataset)
    (tc/column-names))
(:name :group-id :data)

Content of the grouped dataset

(tc/columns (tc/group-by DS :V1) :as-map)
loading …

Grouped dataset as map

(keys (tc/group-by DS :V1 {:result-type :as-map}))
(1 2)
(vals (tc/group-by DS :V1 {:result-type :as-map}))
loading …

Group dataset as map of indexes (row ids)

(tc/group-by DS :V1 {:result-type :as-indexes})
{1 [0 2 4 6 8], 2 [1 3 5 7]}

Grouped datasets are printed as follows by default.

(tc/group-by DS :V1)
loading …

To get groups as sequence or a map can be done from grouped dataset using groups->seq and groups->map functions.

Groups as seq can be obtained by just accessing :data column.

I will use temporary dataset here.

(let [ds (-> {"a" [1 1 2 2]
              "b" ["a" "b" "c" "d"]}
             (tc/dataset)
             (tc/group-by "a"))]
  (seq (ds :data)))
loading …

seq is not necessary but Markdown treats :data as command here

(-> {"a" [1 1 2 2]
     "b" ["a" "b" "c" "d"]}
    (tc/dataset)
    (tc/group-by "a")
    (tc/groups->seq))
loading …

Groups as map

(-> {"a" [1 1 2 2]
     "b" ["a" "b" "c" "d"]}
    (tc/dataset)
    (tc/group-by "a")
    (tc/groups->map))
loading …

Grouping by more than one column. You can see that group names are maps. When ungrouping is done these maps are used to restore column names.

(tc/group-by DS [:V1 :V3] {:result-type :as-seq})
loading …

Grouping can be done by providing just row indexes. This way you can assign the same row to more than one group.

(tc/group-by DS {"group-a" [1 2 1 2]
                  "group-b" [5 5 5 1]} {:result-type :as-seq})
loading …

You can group by a result of grouping function which gets row as map and should return group name. When map is used as a group name, ungrouping restore original column names.

(tc/group-by DS (fn [row] (* (:V1 row)
                             (:V3 row))) {:result-type :as-seq})
loading …

You can use any predicate on column to split dataset into two groups.

(tc/group-by DS (comp #(< % 1.0) :V3) {:result-type :as-seq})
loading …

juxt is also helpful

(tc/group-by DS (juxt :V1 :V3) {:result-type :as-seq})
loading …

tech.ml.dataset provides an option to limit columns which are passed to grouping functions. It’s done for performance purposes.

(tc/group-by DS identity {:result-type :as-seq
                           :select-keys [:V1]})
loading …

Ungrouping

Ungrouping simply concats all the groups into the dataset. Following options are possible

  • :order? - order groups according to the group name ascending order. Default: false
  • :add-group-as-column - should group name become a column? If yes column is created with provided name (or :$group-name if argument is true). Default: nil.
  • :add-group-id-as-column - should group id become a column? If yes column is created with provided name (or :$group-id if argument is true). Default: nil.
  • :dataset-name - to name resulting dataset. Default: nil (_unnamed)

If group name is a map, it will be splitted into separate columns. Be sure that groups (subdatasets) doesn’t contain the same columns already.

If group name is a vector, it will be splitted into separate columns. If you want to name them, set vector of target column names as :add-group-as-column argument.

After ungrouping, order of the rows is kept within the groups but groups are ordered according to the internal storage.


Grouping and ungrouping.

(-> DS
    (tc/group-by :V3)
    (tc/ungroup))
loading …

Groups sorted by group name and named.

(-> DS
    (tc/group-by :V3)
    (tc/ungroup {:order? true
                  :dataset-name "Ordered by V3"}))
loading …

Groups sorted descending by group name and named.

(-> DS
    (tc/group-by :V3)
    (tc/ungroup {:order? :desc
                  :dataset-name "Ordered by V3 descending"}))
loading …

Let’s add group name and id as additional columns

(-> DS
    (tc/group-by (comp #(< % 4) :V2))
    (tc/ungroup {:add-group-as-column true
                  :add-group-id-as-column true}))
loading …

Let’s assign different column names

(-> DS
    (tc/group-by (comp #(< % 4) :V2))
    (tc/ungroup {:add-group-as-column "Is V2 less than 4?"
                  :add-group-id-as-column "group id"}))
loading …

If we group by map, we can automatically create new columns out of group names.

(-> DS
    (tc/group-by (fn [row] {"V1 and V3 multiplied" (* (:V1 row)
                                                      (:V3 row))
                            "V4 as lowercase" (clojure.string/lower-case (:V4 row))}))
    (tc/ungroup {:add-group-as-column true}))
loading …

We can add group names without separation

(-> DS
    (tc/group-by (fn [row] {"V1 and V3 multiplied" (* (:V1 row)
                                                      (:V3 row))
                            "V4 as lowercase" (clojure.string/lower-case (:V4 row))}))
    (tc/ungroup {:add-group-as-column "just map"
                  :separate? false}))
loading …

The same applies to group names as sequences

(-> DS
    (tc/group-by (juxt :V1 :V3))
    (tc/ungroup {:add-group-as-column "abc"}))
loading …

Let’s provide column names

(-> DS
    (tc/group-by (juxt :V1 :V3))
    (tc/ungroup {:add-group-as-column ["v1" "v3"]}))
loading …

Also we can supress separation

(-> DS
    (tc/group-by (juxt :V1 :V3))
    (tc/ungroup {:separate? false
                  :add-group-as-column true}))
loading …

=> _unnamed [9 5]:

Other functions

To check if dataset is grouped or not just use grouped? function.

(tc/grouped? DS)
nil
(tc/grouped? (tc/group-by DS :V1))
true

If you want to remove grouping annotation (to make all the functions work as with regular dataset) you can use unmark-group or as-regular-dataset (alias) functions.

It can be important when you want to remove some groups (rows) from grouped dataset using drop-rows or something like that.

(-> DS
    (tc/group-by :V1)
    (tc/as-regular-dataset)
    (tc/grouped?))
nil

You can also operate on grouped dataset as a regular one in case you want to access its columns using without-grouping-> threading macro.

(-> DS
    (tc/group-by [:V4 :V1])
    (tc/without-grouping->
     (tc/order-by (comp (juxt :V4 :V1) :name))))
loading …

This is considered internal.

If you want to implement your own mapping function on grouped dataset you can call process-group-data and pass function operating on datasets. Result should be a dataset to have ungrouping working.

(-> DS
    (tc/group-by :V1)
    (tc/process-group-data #(str "Shape: " (vector (tc/row-count %) (tc/column-count %))))
    (tc/as-regular-dataset))
loading …

Columns

Column is a special tech.ml.dataset structure based on tech.ml.datatype library. For our purposes we cat treat columns as typed and named sequence bound to particular dataset.

Type of the data is inferred from a sequence during column creation.

Names

To select dataset columns or column names columns-selector is used. columns-selector can be one of the following:

  • :all keyword - selects all columns
  • column name - for single column
  • sequence of column names - for collection of columns
  • regex - to apply pattern on column names or datatype
  • filter predicate - to filter column names or datatype
  • type namespaced keyword for specific datatype or group of datatypes

Column name can be anything.

column-names function returns names according to columns-selector and optional meta-field. meta-field is one of the following:

  • :name (default) - to operate on column names
  • :datatype - to operated on column types
  • :all - if you want to process all metadata

Datatype groups are:

  • :type/numerical - any numerical type
  • :type/float - floating point number (:float32 and :float64)
  • :type/integer - any integer
  • :type/datetime - any datetime type

If qualified keyword starts with :!type, complement set is used.


To select all column names you can use column-names function.

(tc/column-names DS)
(:V1 :V2 :V3 :V4)

or

(tc/column-names DS :all)
(:V1 :V2 :V3 :V4)

In case you want to select column which has name :all (or is sequence or map), put it into a vector. Below code returns empty sequence since there is no such column in the dataset.

(tc/column-names DS [:all])
()

Obviously selecting single name returns it’s name if available

(tc/column-names DS :V1)
(:V1)
(tc/column-names DS "no such column")
()

Select sequence of column names.

(tc/column-names DS [:V1 "V2" :V3 :V4 :V5])
(:V1 :V3 :V4)

Select names based on regex, columns ends with 1 or 4

(tc/column-names DS #".*[14]")
(:V1 :V4)

Select names based on regex operating on type of the column (to check what are the column types, call (tc/info DS :columns). Here we want to get integer columns only.

(tc/column-names DS #"^:int.*" :datatype)
(:V1 :V2)

or

(tc/column-names DS :type/integer)
(:V1 :V2)

And finally we can use predicate to select names. Let’s select double precision columns.

(tc/column-names DS #{:float64} :datatype)
(:V3)

or

(tc/column-names DS :type/float64)
(:V3)

If you want to select all columns but given, use complement function. Works only on a predicate.

(tc/column-names DS (complement #{:V1}))
(:V2 :V3 :V4)
(tc/column-names DS (complement #{:float64}) :datatype)
(:V1 :V2 :V4)
(tc/column-names DS :!type/float64)
(:V1 :V2 :V4)

You can select column names based on all column metadata at once by using :all metadata selector. Below we want to select column names ending with 1 which have long datatype.

(tc/column-names DS (fn [meta]
                       (and (= :int64 (:datatype meta))
                            (clojure.string/ends-with? (:name meta) "1"))) :all)
(:V1)

Select

select-columns creates dataset with columns selected by columns-selector as described above. Function works on regular and grouped dataset.


Select only float64 columns

(tc/select-columns DS #(= :float64 %) :datatype)
loading …

or

(tc/select-columns DS :type/float64)
loading …

Select all but :V1 columns

(tc/select-columns DS (complement #{:V1}))
loading …

If we have grouped data set, column selection is applied to every group separately.

(-> DS
    (tc/group-by :V1)
    (tc/select-columns [:V2 :V3])
    (tc/groups->map))
loading …

Drop

drop-columns creates dataset with removed columns.


Drop float64 columns

(tc/drop-columns DS #(= :float64 %) :datatype)
loading …

or

(tc/drop-columns DS :type/float64)
loading …

Drop all columns but :V1 and :V2

(tc/drop-columns DS (complement #{:V1 :V2}))
loading …

If we have grouped data set, column selection is applied to every group separately. Selected columns are dropped.

(-> DS
    (tc/group-by :V1)
    (tc/drop-columns [:V2 :V3])
    (tc/groups->map))
loading …

Rename

If you want to rename colums use rename-columns and pass map where keys are old names, values new ones.

You can also pass mapping function with optional columns-selector

(tc/rename-columns DS {:V1 "v1"
                        :V2 "v2"
                        :V3 [1 2 3]
                        :V4 (Object.)})
loading …

Map all names with function

(tc/rename-columns DS (comp str second name))
loading …

Map selected names with function

(tc/rename-columns DS [:V1 :V3] (comp str second name))
loading …

Function works on grouped dataset

(-> DS
    (tc/group-by :V1)
    (tc/rename-columns {:V1 "v1"
                         :V2 "v2"
                         :V3 [1 2 3]
                         :V4 (Object.)})
    (tc/groups->map))
loading …

Add or update

To add (or replace existing) column call add-column function. Function accepts:

  • ds - a dataset
  • column-name - if it’s existing column name, column will be replaced
  • column - can be column (from other dataset), sequence, single value or function. Too big columns are always trimmed. Too small are cycled or extended with missing values (according to size-strategy argument)
  • size-strategy (optional) - when new column is shorter than dataset row count, following strategies are applied:
    • :cycle - repeat data
    • :na - append missing values
    • :strict - (default) throws an exception when sizes mismatch

Function works on grouped dataset.


Add single value as column

(tc/add-column DS :V5 "X")
loading …

Replace one column (column is trimmed)

^:note-to-test/skip
(tc/add-column DS :V1 (repeatedly rand))
loading …

Copy column

(tc/add-column DS :V5 (DS :V1))
loading …

When function is used, argument is whole dataset and the result should be column, sequence or single value

(tc/add-column DS :row-count tc/row-count)
loading …

Above example run on grouped dataset, applies function on each group separately.

(-> DS
    (tc/group-by :V1)
    (tc/add-column :row-count tc/row-count)
    (tc/ungroup))
loading …

When column which is added is longer than row count in dataset, column is trimmed. When column is shorter, it’s cycled or missing values are appended.

(tc/add-column DS :V5 [:r :b] :cycle)
loading …
(tc/add-column DS :V5 [:r :b] :na)
loading …

Exception is thrown when :strict (default) strategy is used and column size is not equal row count

(try
  (tc/add-column DS :V5 [:r :b])
  (catch Exception e (str "Exception caught: "(ex-message e))))
"Exception caught: Column size (2) should be exactly the same as dataset row count (9). Consider `:cycle` or `:na` strategy."

Tha same applies for grouped dataset

(-> DS
    (tc/group-by :V3)
    (tc/add-column :V5 [:r :b] :na)
    (tc/ungroup))
loading …

Let’s use other column to fill groups

(-> DS
    (tc/group-by :V3)
    (tc/add-column :V5 (DS :V2) :cycle)
    (tc/ungroup))
loading …

In case you want to add or update several columns you can call add-columns and provide map where keys are column names, vals are columns.

(tc/add-columns DS {:V1 #(map inc (% :V1))
                               :V5 #(map (comp keyword str) (% :V4))
                               :V6 11})
loading …

Update

If you want to modify specific column(s) you can call update-columns. Arguments:

  • dataset
  • one of:
    • columns-selector and function (or sequence of functions)
    • map where keys are column names and vals are function

Functions accept column and have to return column or sequence


Reverse of columns

(tc/update-columns DS :all reverse)
loading …

Apply dec/inc on numerical columns

(tc/update-columns DS :type/numerical [(partial map dec)
                                        (partial map inc)])
loading …

You can also assign a function to a column by packing operations into the map.

^:note-to-test/skip
(tc/update-columns DS {:V1 reverse
                       :V2 (comp shuffle seq)})
loading …

Map

The other way of creating or updating column is to map rows as regular map function. The arity of mapping function should be the same as number of selected columns.

Arguments:

  • ds - dataset
  • column-name - target column name
  • columns-selector - columns selected
  • map-fn - mapping function

Let’s add numerical columns together

(tc/map-columns DS
                 :sum-of-numbers
                 (tc/column-names DS  #{:int64 :float64} :datatype)
                 (fn [& rows]
                   (reduce + rows)))
loading …

The same works on grouped dataset

(-> DS
    (tc/group-by :V4)
    (tc/map-columns :sum-of-numbers
                     (tc/column-names DS  #{:int64 :float64} :datatype)
                     (fn [& rows]
                       (reduce + rows)))
    (tc/ungroup))
loading …

Reorder

To reorder columns use columns selectors to choose what columns go first. The unseleted columns are appended to the end.

(tc/reorder-columns DS :V4 [:V3 :V2])
loading …

This function doesn’t let you select meta field, so you have to call column-names in such case. Below we want to add integer columns at the end.

(tc/reorder-columns DS (tc/column-names DS (complement #{:int64}) :datatype))
loading …

Type conversion

To convert column into given datatype can be done using convert-types function. Not all the types can be converted automatically also some types require slow parsing (every conversion from string). In case where conversion is not possible you can pass conversion function.

Arguments:

  • ds - dataset
  • Two options:
    • coltype-map in case when you want to convert several columns, keys are column names, vals are new types
    • column-selector and new-types - column name and new datatype (or datatypes as sequence)

new-types can be:

  • a type like :int64 or :string or sequence of types
  • or sequence of pair of datetype and conversion function

After conversion additional infomation is given on problematic values.

The other conversion is casting column into java array (->array) of the type column or provided as argument. Grouped dataset returns sequence of arrays.


Basic conversion

(-> DS
    (tc/convert-types :V1 :float64)
    (tc/info :columns))
loading …

Using custom converter. Let’s treat :V4 as haxadecimal values. See that this way we can map column to any value.

(-> DS
    (tc/convert-types :V4 [[:int16 #(Integer/parseInt % 16)]]))
loading …

You can process several columns at once

(-> DS
    (tc/convert-types {:V1 :float64
                        :V2 :object
                        :V3 [:boolean #(< % 1.0)]
                        :V4 :object})
    (tc/info :columns))
loading …

Convert one type into another

(-> DS
    (tc/convert-types :type/numerical :int16)
    (tc/info :columns))
loading …

Function works on the grouped dataset

(-> DS
    (tc/group-by :V1)
    (tc/convert-types :V1 :float32)
    (tc/ungroup)
    (tc/info :columns))
loading …

Double array conversion.

(tc/->array DS :V1)
[1, 2, 1, 2, 1, 2, 1, 2, 1]

Function also works on grouped dataset

(-> DS
    (tc/group-by :V3)
    (tc/->array :V2))
([1, 4, 7] [2, 5, 8] [3, 6, 9])

You can also cast the type to the other one (if casting is possible):

(tc/->array DS :V4 :string)
["A", "B", "C", "A", "B", "C", "A", "B", "C"]
(tc/->array DS :V1 :float32)
[1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0]

Rows

Rows can be selected or dropped using various selectors:

  • row id(s) - row index as number or seqence of numbers (first row has index 0, second 1 and so on)
  • sequence of true/false values
  • filter by predicate (argument is row as a map)

When predicate is used you may want to limit columns passed to the function (select-keys option).

Additionally you may want to precalculate some values which will be visible for predicate as additional columns. It’s done internally by calling add-columns on a dataset. :pre is used as a column definitions.

Select

Select fifth row

(tc/select-rows DS 4)
loading …

Select 3 rows

(tc/select-rows DS [1 4 5])
loading …

Select rows using sequence of true/false values

(tc/select-rows DS [true nil nil true])
loading …

Select rows using predicate

(tc/select-rows DS (comp #(< % 1) :V3))
loading …

The same works on grouped dataset, let’s select first row from every group.

(-> DS
    (tc/group-by :V1)
    (tc/select-rows 0)
    (tc/ungroup))
loading …

If you want to select :V2 values which are lower than or equal mean in grouped dataset you have to precalculate it using :pre.

(-> DS
    (tc/group-by :V4)
    (tc/select-rows (fn [row] (<= (:V2 row) (:mean row)))
                     {:pre {:mean #(tech.v3.datatype.functional/mean (% :V2))}})
    (tc/ungroup))
loading …

Drop

drop-rows removes rows, and accepts exactly the same parameters as select-rows


Drop values lower than or equal :V2 column mean in grouped dataset.

(-> DS
    (tc/group-by :V4)
    (tc/drop-rows (fn [row] (<= (:V2 row) (:mean row)))
                   {:pre {:mean #(tech.v3.datatype.functional/mean (% :V2))}})
    (tc/ungroup))
loading …

Map rows

Call a mapping function for every row. Mapping function should return a map, where keys are column names (new or old) and values are column values.

Works on grouped dataset too.

(tc/map-rows DS (fn [{:keys [V1 V2]}] {:V1 0
                                       :V5 (/ (+ V1 V2) (double V2))}))
loading …

Other

There are several function to select first, last, random rows, or display head, tail of the dataset. All functions work on grouped dataset.

All random functions accept :seed as an option if you want to fix returned result.


First row

(tc/first DS)
loading …

Last row

(tc/last DS)
loading …

Random row (single)

^:note-to-test/skip
(tc/rand-nth DS)
loading …

Random row (single) with seed

(tc/rand-nth DS {:seed 42})
loading …

Random n (default: row count) rows with repetition.

^:note-to-test/skip
(tc/random DS)
loading …

Five random rows with repetition

^:note-to-test/skip
(tc/random DS 5)
loading …

Five random, non-repeating rows

^:note-to-test/skip
(tc/random DS 5 {:repeat? false})
loading …

Five random, with seed

(tc/random DS 5 {:seed 42})
loading …

Shuffle dataset

^:note-to-test/skip
(tc/shuffle DS)
loading …

Shuffle with seed

(tc/shuffle DS {:seed 42})
loading …

First n rows (default 5)

(tc/head DS)
loading …

Last n rows (default 5)

(tc/tail DS)
loading …

by-rank calculates rank on column(s). It’s base on R rank() with addition of :dense (default) tie strategy which give consecutive rank numbering.

:desc? options (default: true) sorts input with descending order, giving top values under 0 value.

rank is zero based and is defined at tablecloth.api.utils namespace.


(tc/by-rank DS :V3 zero?)
loading …

most V3 values

(tc/by-rank DS :V3 zero? {:desc? false})
loading …

least V3 values


Rank also works on multiple columns

(tc/by-rank DS [:V1 :V3] zero? {:desc? false})
loading …

Select 5 random rows from each group

^:note-to-test/skip
(-> DS
    (tc/group-by :V4)
    (tc/random 5)
    (tc/ungroup))
loading …

Aggregate

Aggregating is a function which produces single row out of dataset.

Aggregator is a function or sequence or map of functions which accept dataset as an argument and result single value, sequence of values or map.

Where map is given as an input or result, keys are treated as column names.

Grouped dataset is ungrouped after aggreation. This can be turned off by setting :ungroup to false. In case you want to pass additional ungrouping parameters add them to the options.

By default resulting column names are prefixed with summary prefix (set it with :default-column-name-prefix option).


Let’s calculate mean of some columns

(tc/aggregate DS #(reduce + (% :V2)))
loading …

Let’s give resulting column a name.

(tc/aggregate DS {:sum-of-V2 #(reduce + (% :V2))})
loading …

Sequential result is spread into separate columns

(tc/aggregate DS #(take 5(% :V2)))
loading …

You can combine all variants and rename default prefix

(tc/aggregate DS [#(take 3 (% :V2))
                   (fn [ds] {:sum-v1 (reduce + (ds :V1))
                            :prod-v3 (reduce * (ds :V3))})] {:default-column-name-prefix "V2-value"})
loading …

Processing grouped dataset

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate [#(take 3 (% :V2))
                    (fn [ds] {:sum-v1 (reduce + (ds :V1))
                             :prod-v3 (reduce * (ds :V3))})] {:default-column-name-prefix "V2-value"}))
loading …

Result of aggregating is automatically ungrouped, you can skip this step by stetting :ungroup option to false.

(-> DS
    (tc/group-by [:V3])
    (tc/aggregate [#(take 3 (% :V2))
                    (fn [ds] {:sum-v1 (reduce + (ds :V1))
                             :prod-v3 (reduce * (ds :V3))})] {:default-column-name-prefix "V2-value"
                                                              :ungroup? false}))
loading …

Column

You can perform columnar aggreagation also. aggregate-columns selects columns and apply aggregating function (or sequence of functions) for each column separately.

(tc/aggregate-columns DS [:V1 :V2 :V3] #(reduce + %))
loading …

(tc/aggregate-columns DS [:V1 :V2 :V3] [#(reduce + %)
                                         #(reduce max %)
                                         #(reduce * %)])
loading …

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate-columns [:V1 :V2 :V3] #(reduce + %)))
loading …

You can also aggregate whole dataset

(-> DS
    (tc/drop-columns :V4)
    (tc/aggregate-columns #(reduce + %)))
loading …

Crosstab

Cross tabulation built from two sets of columns. First rows and cols are used to construct grouped dataset, then aggregation function is applied for each pair. By default it counts rows from each group.

Options are:

  • :aggregator - function which aggregates values of grouped dataset, default it’s row-count
  • :marginal-rows and :marginal-cols - if true, sum of rows and cols are added as an additional columns and row. May be custom function which accepts pure row and col as a seq.
  • :replace-missing? - should missing values be replaced (default: true) with :missing-value (default: 0)
  • :pivot? - if false, flat aggregation result is returned (default: false)
(def ctds (tc/dataset {:a [:foo :foo :bar :bar :foo :foo]
                       :b [:one :one :two :one :two :one]
                       :c [:dull :dull :shiny :dull :dull :shiny]}))
ctds
loading …

(tc/crosstab ctds :a [:b :c])
loading …

With marginals

(tc/crosstab ctds :a [:b :c] {:marginal-rows true :marginal-cols true})
loading …

Set missing value to -1

(tc/crosstab ctds :a [:b :c] {:missing-value -1})
loading …

Turn off pivoting

(tc/crosstab ctds :a [:b :c] {:pivot? false})
loading …

Order

Ordering can be done by column(s) or any function operating on row. Possible order can be:

  • :asc for ascending order (default)
  • :desc for descending order
  • custom comparator

:select-keys limits row map provided to ordering functions.


Order by single column, ascending

(tc/order-by DS :V1)
loading …

Descending order

(tc/order-by DS :V1 :desc)
loading …

Order by two columns

(tc/order-by DS [:V1 :V2])
loading …

Use different orders for columns

(tc/order-by DS [:V1 :V2] [:asc :desc])
loading …
(tc/order-by DS [:V1 :V2] [:desc :desc])
loading …
(tc/order-by DS [:V1 :V3] [:desc :asc])
loading …

Custom function can be used to provided ordering key. Here order by :V4 descending, then by product of other columns ascending.

(tc/order-by DS [:V4 (fn [row] (* (:V1 row)
                                  (:V2 row)
                                  (:V3 row)))] [:desc :asc])
loading …

Custom comparator also can be used in case objects are not comparable by default. Let’s define artificial one: if Euclidean distance is lower than 2, compare along z else along x and y. We use first three columns for that.

(defn dist
  [v1 v2]
  (->> v2
       (map - v1)
       (map #(* % %))
       (reduce +)
       (Math/sqrt)))
(tc/order-by DS [:V1 :V2 :V3] (fn [[x1 y1 z1 :as v1] [x2 y2 z2 :as v2]]
                                (let [d (dist v1 v2)]
                                  (if (< d 2.0)
                                    (compare z1 z2)
                                    (compare [x1 y1] [x2 y2])))))
loading …

Unique

Remove rows which contains the same data. By default unique-by removes duplicates from whole dataset. You can also pass list of columns or functions (similar as in group-by) to remove duplicates limited by them. Default strategy is to keep the first row. More strategies below.

unique-by works on groups


Remove duplicates from whole dataset

(tc/unique-by DS)
loading …

Remove duplicates from each group selected by column.

(tc/unique-by DS :V1)
loading …

Pair of columns

(tc/unique-by DS [:V1 :V3])
loading …

Also function can be used, split dataset by modulo 3 on columns :V2

(tc/unique-by DS (fn [m] (mod (:V2 m) 3)))
loading …

The same can be achived with group-by

(-> DS
    (tc/group-by (fn [m] (mod (:V2 m) 3)))
    (tc/first)
    (tc/ungroup))
loading …

Grouped dataset

(-> DS
    (tc/group-by :V4)
    (tc/unique-by :V1)
    (tc/ungroup))
loading …

Strategies

There are 4 strategies defined:

  • :first - select first row (default)
  • :last - select last row
  • :random - select random row
  • any function - apply function to a columns which are subject of uniqueness

Last

(tc/unique-by DS :V1 {:strategy :last})
loading …

Random

^:note-to-test/skip
(tc/unique-by DS :V1 {:strategy :random})
loading …

Pack columns into vector

(tc/unique-by DS :V4 {:strategy vec})
loading …

Sum columns

(tc/unique-by DS :V4 {:strategy (partial reduce +)})
loading …

Group by function and apply functions

(tc/unique-by DS (fn [m] (mod (:V2 m) 3)) {:strategy vec})
loading …

Grouped dataset

(-> DS
    (tc/group-by :V1)
    (tc/unique-by (fn [m] (mod (:V2 m) 3)) {:strategy vec})
    (tc/ungroup {:add-group-as-column :from-V1}))
loading …

Missing

When dataset contains missing values you can select or drop rows with missing values or replace them using some strategy.

column-selector can be used to limit considered columns

Let’s define dataset which contains missing values

(def DSm (tc/dataset {:V1 (take 9 (cycle [1 2 nil]))
                      :V2 (range 1 10)
                      :V3 (take 9 (cycle [0.5 1.0 nil 1.5]))
                      :V4 (take 9 (cycle ["A" "B" "C"]))}))
DSm
loading …

Select

Select rows with missing values

(tc/select-missing DSm)
loading …

Select rows with missing values in :V1

(tc/select-missing DSm :V1)
loading …

The same with grouped dataset

(-> DSm
    (tc/group-by :V4)
    (tc/select-missing :V3)
    (tc/ungroup))
loading …

Drop

Drop rows with missing values

(tc/drop-missing DSm)
loading …

Drop rows with missing values in :V1

(tc/drop-missing DSm :V1)
loading …

The same with grouped dataset

(-> DSm
    (tc/group-by :V4)
    (tc/drop-missing :V1)
    (tc/ungroup))
loading …

Replace

Missing values can be replaced using several strategies. replace-missing accepts:

  • dataset
  • column selector, default: :all
  • strategy, default: :nearest
  • value (optional)
    • single value
    • sequence of values (cycled)
    • function, applied on column(s) with stripped missings
    • map with [index,value] pairs

Strategies are:

  • :value - replace with given value
  • :up - copy values up
  • :down - copy values down
  • :updown - copy values up and then down for missing values at the end
  • :downup - copy values down and then up for missing values at the beginning
  • :mid or :nearest - copy values around known values
  • :midpoint - use average value from previous and next non-missing
  • :lerp - trying to lineary approximate values, works for numbers and datetime, otherwise applies :nearest. For numbers always results in float datatype.

Let’s define special dataset here:

(def DSm2 (tc/dataset {:a [nil nil nil 1.0 2  nil nil nil nil  nil 4   nil  11 nil nil]
                       :b [2   2   2 nil nil nil nil nil nil 13   nil   3  4  5 5]}))
DSm2
loading …

Replace missing with default strategy for all columns

(tc/replace-missing DSm2)
loading …

Replace missing with single value in whole dataset

(tc/replace-missing DSm2 :all :value 999)
loading …

Replace missing with single value in :a column

(tc/replace-missing DSm2 :a :value 999)
loading …

Replace missing with sequence in :a column

(tc/replace-missing DSm2 :a :value [-999 -998 -997])
loading …

Replace missing with a function (mean)

(tc/replace-missing DSm2 :a :value tech.v3.datatype.functional/mean)
loading …

Replace missing some missing values with a map

(tc/replace-missing DSm2 :a :value {0 100 1 -100 14 -1000})
loading …

Using :down strategy, fills gaps with values from above. You can see that if missings are at the beginning, the are filled with first value

(tc/replace-missing DSm2 [:a :b] :downup)
loading …

To fix above issue you can provide value

(tc/replace-missing DSm2 [:a :b] :down 999)
loading …

The same applies for :up strategy which is opposite direction.

(tc/replace-missing DSm2 [:a :b] :up)
loading …

(tc/replace-missing DSm2 [:a :b] :updown)
loading …

The same applies for :up strategy which is opposite direction.

(tc/replace-missing DSm2 [:a :b] :midpoint)
loading …

We can use a function which is applied after applying :up or :down

(tc/replace-missing DSm2 [:a :b] :down tech.v3.datatype.functional/mean)
loading …

Lerp tries to apply linear interpolation of the values

(tc/replace-missing DSm2 [:a :b] :lerp)
loading …

Lerp works also on dates

(-> (tc/dataset {:dt [(java.time.LocalDateTime/of 2020 1 1 11 22 33)
                      nil nil nil nil nil nil nil
                      (java.time.LocalDateTime/of 2020 10 1 1 1 1)]})
    (tc/replace-missing :lerp))
loading …

Inject

When your column contains not continuous data range you can fill up with lacking values. Arguments:

  • dataset
  • column name
  • expected step (max-span, milliseconds in case of datetime column)
  • (optional) missing-strategy - how to replace missing, default :down (set to nil if none)
  • (optional) missing-value - optional value for replace missing

(-> (tc/dataset {:a [1 2 9]
                 :b [:a :b :c]})
    (tc/fill-range-replace :a 1))
loading …

Join/Separate Columns

Joining or separating columns are operations which can help to tidy messy dataset.

  • join-columns joins content of the columns (as string concatenation or other structure) and stores it in new column
  • separate-column splits content of the columns into set of new columns

Join

join-columns accepts:

  • dataset
  • column selector (as in select-columns)
  • options
  • :separator (default "-")
  • :drop-columns? - whether to drop source columns or not (default true)
  • :result-type
  • :map - packs data into map
  • :seq - packs data into sequence
  • :string - join strings with separator (default)
  • or custom function which gets row as a vector
  • :missing-subst - substitution for missing value

Default usage. Create :joined column out of other columns.

(tc/join-columns DSm :joined [:V1 :V2 :V4])
loading …

Without dropping source columns.

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:drop-columns? false})
loading …

Let’s replace missing value with “NA” string.

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:missing-subst "NA"})
loading …

We can use custom separator.

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:separator "/"
                                            :missing-subst "."})
loading …

Or even sequence of separators.

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:separator ["-" "/"]
                                            :missing-subst "."})
loading …

The other types of results, map:

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:result-type :map})
loading …

Sequence

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:result-type :seq})
loading …

Custom function, calculate hash

(tc/join-columns DSm :joined [:V1 :V2 :V4] {:result-type hash})
loading …

Grouped dataset

(-> DSm
    (tc/group-by :V4)
    (tc/join-columns :joined [:V1 :V2 :V4])
    (tc/ungroup))
loading …

Tidyr examples

source

(def df (tc/dataset {:x ["a" "a" nil nil]
                      :y ["b" nil "b" nil]}))
df
loading …

(tc/join-columns df "z" [:x :y] {:drop-columns? false
                                  :missing-subst "NA"
                                  :separator "_"})
loading …

(tc/join-columns df "z" [:x :y] {:drop-columns? false
                                  :separator "_"})
loading …

Separate

Column can be also separated into several other columns using string as separator, regex or custom function. Arguments:

  • dataset
  • source column
  • target columns - can be nil or :infer to automatically create columns
  • separator as:
    • string - it’s converted to regular expression and passed to clojure.string/split function
    • regex
    • or custom function (default: identity)
  • options
    • :drop-columns? - whether drop source column(s) or not (default: true). Set to :all to keep only separation result.
    • :missing-subst - values which should be treated as missing, can be set, sequence, value or function (default: "")

Custom function (as separator) should return seqence of values for given value or a sequence of map.


Separate float into integer and factional values

(tc/separate-column DS :V3 [:int-part :frac-part] (fn [^double v]
                                                     [(int (quot v 1.0))
                                                      (mod v 1.0)]))
loading …

Source column can be kept

(tc/separate-column DS :V3 [:int-part :frac-part] (fn [^double v]
                                                     [(int (quot v 1.0))
                                                      (mod v 1.0)]) {:drop-column? false})
loading …

We can treat 0 or 0.0 as missing value

(tc/separate-column DS :V3 [:int-part :frac-part] (fn [^double v]
                                                     [(int (quot v 1.0))
                                                      (mod v 1.0)]) {:missing-subst [0 0.0]})
loading …

Works on grouped dataset

(-> DS
    (tc/group-by :V4)
    (tc/separate-column :V3 [:int-part :fract-part] (fn [^double v]
                                                       [(int (quot v 1.0))
                                                        (mod v 1.0)]))
    (tc/ungroup))
loading …

Separate using separator returning sequence of maps.

(tc/separate-column DS :V3 (fn [^double v]
                              {:int-part (int (quot v 1.0))
                               :fract-part (mod v 1.0)}))
loading …

Keeping all columns

(tc/separate-column DS :V3 nil (fn [^double v]
                                  {:int-part (int (quot v 1.0))
                                   :fract-part (mod v 1.0)}) {:drop-column? false})
loading …

Droping all colums but separated

(tc/separate-column DS :V3 nil (fn [^double v]
                                 {:int-part (int (quot v 1.0))
                                  :fract-part (mod v 1.0)}) {:drop-column? :all})
loading …

Infering column names

(tc/separate-column DS :V3 (fn [^double v]
                             [(int (quot v 1.0)) (mod v 1.0)]))
loading …

Join and separate together.

(-> DSm
    (tc/join-columns :joined [:V1 :V2 :V4] {:result-type :map})
    (tc/separate-column :joined [:v1 :v2 :v4] (juxt :V1 :V2 :V4)))
loading …
(-> DSm
    (tc/join-columns :joined [:V1 :V2 :V4] {:result-type :seq})
    (tc/separate-column :joined [:v1 :v2 :v4] identity))
loading …
Tidyr examples

separate source extract source

(def df-separate (tc/dataset {:x [nil "a.b" "a.d" "b.c"]}))
(def df-separate2 (tc/dataset {:x ["a" "a b" nil "a b c"]}))
(def df-separate3 (tc/dataset {:x ["a?b" nil "a.b" "b:c"]}))
(def df-extract (tc/dataset {:x [nil "a-b" "a-d" "b-c" "d-e"]}))
df-separate
loading …
df-separate2
loading …
df-separate3
loading …
df-extract
loading …

(tc/separate-column df-separate :x [:A :B] "\\.")
loading …

You can drop columns after separation by setting nil as a name. We need second value here.

(tc/separate-column df-separate :x [nil :B] "\\.")
loading …

Extra data is dropped

(tc/separate-column df-separate2 :x ["a" "b"] " ")
loading …

Split with regular expression

(tc/separate-column df-separate3 :x ["a" "b"] "[?\\.:]")
loading …

Or just regular expression to extract values

(tc/separate-column df-separate3 :x ["a" "b"] #"(.).(.)")
loading …

Extract first value only

(tc/separate-column df-extract :x ["A"] "-")
loading …

Split with regex

(tc/separate-column df-extract :x ["A" "B"] #"(\p{Alnum})-(\p{Alnum})")
loading …

Only a,b,c,d strings

(tc/separate-column df-extract :x ["A" "B"] #"([a-d]+)-([a-d]+)")
loading …

Array column conversion

A dataset can have as well columns of type java array. We can convert from normal columns to a single array column and back like this:

(-> (tc/dataset {:x [(double-array [1 2 3])
                     (double-array [4 5 6])]
                 :y [:a :b]})
    (tc/array-column->columns :x))
loading …

and the other way around:

(-> (tc/dataset {0 [0.0 1 2]
                 1 [3.0 4 5]
                 :x [:a :b :c]})
    (tc/columns->array-column [0 1] :y))
loading …

Fold/Unroll Rows

To pack or unpack the data into single value you can use fold-by and unroll functions.

fold-by groups dataset and packs columns data from each group separately into desired datastructure (like vector or sequence). unroll does the opposite.

Fold-by

Group-by and pack columns into vector

(tc/fold-by DS [:V3 :V4 :V1])
loading …

You can pack several columns at once.

(tc/fold-by DS [:V4])
loading …

You can use custom packing function

(tc/fold-by DS [:V4] seq)
loading …

or

(tc/fold-by DS [:V4] set)
loading …

This works also on grouped dataset

(-> DS
    (tc/group-by :V1)
    (tc/fold-by :V4)
    (tc/ungroup))
loading …

Unroll

unroll unfolds sequences stored in data, multiplying other ones when necessary. You can unroll more than one column at once (folded data should have the same size!).

Options:

  • :indexes? if true (or column name), information about index of unrolled sequence is added.
  • :datatypes list of datatypes which should be applied to restored columns, a map

Unroll one column

(tc/unroll (tc/fold-by DS [:V4]) [:V1])
loading …

Unroll all folded columns

(tc/unroll (tc/fold-by DS [:V4]) [:V1 :V2 :V3])
loading …

Unroll one by one leads to cartesian product

(-> DS
    (tc/fold-by [:V4 :V1])
    (tc/unroll [:V2])
    (tc/unroll [:V3]))
loading …

You can add indexes

(tc/unroll (tc/fold-by DS [:V1]) [:V4 :V2 :V3] {:indexes? true})
loading …
(tc/unroll (tc/fold-by DS [:V1]) [:V4 :V2 :V3] {:indexes? "vector idx"})
loading …

You can also force datatypes

(-> DS
    (tc/fold-by [:V1])
    (tc/unroll [:V4 :V2 :V3] {:datatypes {:V4 :string
                                           :V2 :int16
                                           :V3 :float32}})
    (tc/info :columns))
loading …

This works also on grouped dataset

(-> DS
    (tc/group-by :V1)
    (tc/fold-by [:V1 :V4])
    (tc/unroll :V3 {:indexes? true})
    (tc/ungroup))
loading …

Reshape

Reshaping data provides two types of operations:

  • pivot->longer - converting columns to rows
  • pivot->wider - converting rows to columns

Both functions are inspired on tidyr R package and provide almost the same functionality.

All examples are taken from mentioned above documentation.

Both functions work only on regular dataset.

Longer

pivot->longer converts columns to rows. Column names are treated as data.

Arguments:

  • dataset
  • columns selector
  • options:
    • :target-columns - names of the columns created or columns pattern (see below) (default: :$column)
    • :value-column-name - name of the column for values (default: :$value)
    • :splitter - string, regular expression or function which splits source column names into data
    • :drop-missing? - remove rows with missing? (default: true)
    • :datatypes - map of target columns data types
    • :coerce-to-number - try to convert extracted values to numbers if possible (default: true)

:target-columns - can be:

  • column name - source columns names are put there as a data
  • column names as seqence - source columns names after split are put separately into :target-columns as data
  • pattern - is a sequence of names, where some of the names are nil. nil is replaced by a name taken from splitter and such column is used for values.

Create rows from all columns but "religion".

(def relig-income (tc/dataset "data/relig_income.csv"))
relig-income
loading …
(tc/pivot->longer relig-income (complement #{"religion"}))
loading …

Convert only columns starting with "wk" and pack them into :week column, values go to :rank column

(def bilboard (-> (tc/dataset "data/billboard.csv.gz")
                  (tc/drop-columns :type/boolean)))

drop some boolean columns, tidyr just skips them

(->> bilboard
     (tc/column-names)
     (take 13)
     (tc/select-columns bilboard))
loading …
(tc/pivot->longer bilboard #(clojure.string/starts-with? % "wk") {:target-columns :week
                                                                   :value-column-name :rank})
loading …

We can create numerical column out of column names

(tc/pivot->longer bilboard #(clojure.string/starts-with? % "wk") {:target-columns :week
                                                                   :value-column-name :rank
                                                                   :splitter #"wk(.*)"
                                                                   :datatypes {:week :int16}})
loading …

When column names contain observation data, such column names can be splitted and data can be restored into separate columns.

(def who (tc/dataset "data/who.csv.gz"))
(->> who
     (tc/column-names)
     (take 10)
     (tc/select-columns who))
loading …
(tc/pivot->longer who #(clojure.string/starts-with? % "new") {:target-columns [:diagnosis :gender :age]
                                                               :splitter #"new_?(.*)_(.)(.*)"
                                                               :value-column-name :count})
loading …

When data contains multiple observations per row, we can use splitter and pattern for target columns to create new columns and put values there. In following dataset we have two obseravations dob and gender for two childs. We want to put child infomation into the column and leave dob and gender for values.

(def family (tc/dataset "data/family.csv"))
family
loading …
(tc/pivot->longer family (complement #{"family"}) {:target-columns [nil :child]
                                                    :splitter "_"
                                                    :datatypes {"gender" :int16}})
loading …

Similar here, we have two observations: x and y in four groups.

(def anscombe (tc/dataset "data/anscombe.csv"))
anscombe
loading …
(tc/pivot->longer anscombe :all {:splitter #"(.)(.)"
                                  :target-columns [nil :set]})
loading …

^:note-to-test/skip
(def pnl (tc/dataset {:x [1 2 3 4]
                      :a [1 1 0 0]
                      :b [0 1 1 1]
                      :y1 (repeatedly 4 rand)
                      :y2 (repeatedly 4 rand)
                      :z1 [3 3 3 3]
                      :z2 [-2 -2 -2 -2]}))
^:note-to-test/skip
pnl
loading …
^:note-to-test/skip
(tc/pivot->longer pnl [:y1 :y2 :z1 :z2] {:target-columns [nil :times]
                                         :splitter #":(.)(.)"})
loading …

Wider

pivot->wider converts rows to columns.

Arguments:

  • dataset
  • columns-selector - values from selected columns are converted to new columns
  • value-columns - what are values

When multiple columns are used as columns selector, names are joined using :concat-columns-with option. :concat-columns-with can be a string or function (default: “_“). Function accepts sequence of names.

When columns-selector creates non unique set of values, they are folded using :fold-fn (default: vec) option.

When value-columns is a sequence, multiple observations as columns are created appending value column names into new columns. Column names are joined using :concat-value-with option. :concat-value-with can be a string or function (default: “-”). Function accepts current column name and value.


Use station as a name source for columns and seen for values

(def fish (tc/dataset "data/fish_encounters.csv"))
fish
loading …
(tc/pivot->wider fish "station" "seen" {:drop-missing? false})
loading …

If selected columns contain multiple values, such values should be folded.

(def warpbreaks (tc/dataset "data/warpbreaks.csv"))
warpbreaks
loading …

Let’s see how many values are for each type of wool and tension groups

(-> warpbreaks
    (tc/group-by ["wool" "tension"])
    (tc/aggregate {:n tc/row-count}))
loading …
(-> warpbreaks
    (tc/reorder-columns ["wool" "tension" "breaks"])
    (tc/pivot->wider "wool" "breaks" {:fold-fn vec}))
loading …

We can also calculate mean (aggreate values)

(-> warpbreaks
    (tc/reorder-columns ["wool" "tension" "breaks"])
    (tc/pivot->wider "wool" "breaks" {:fold-fn tech.v3.datatype.functional/mean}))
loading …

Multiple source columns, joined with default separator.

(def production (tc/dataset "data/production.csv"))
production
loading …
(tc/pivot->wider production ["product" "country"] "production")
loading …

Joined with custom function

(tc/pivot->wider production ["product" "country"] "production" {:concat-columns-with vec})
loading …

Multiple value columns

(def income (tc/dataset "data/us_rent_income.csv"))
income
loading …
(tc/pivot->wider income "variable" ["estimate" "moe"] {:drop-missing? false})
loading …

Value concatenated by custom function

(tc/pivot->wider income "variable" ["estimate" "moe"] {:concat-columns-with vec
                                                        :concat-value-with vector
                                                        :drop-missing? false})
loading …

Reshape contact data

(def contacts (tc/dataset "data/contacts.csv"))
contacts
loading …
(tc/pivot->wider contacts "field" "value" {:drop-missing? false})
loading …

Reshaping

A couple of tidyr examples of more complex reshaping.


World bank

(def world-bank-pop (tc/dataset "data/world_bank_pop.csv.gz"))
(->> world-bank-pop
     (tc/column-names)
     (take 8)
     (tc/select-columns world-bank-pop))
loading …

Step 1 - convert years column into values

(def pop2 (tc/pivot->longer world-bank-pop (map str (range 2000 2018)) {:drop-missing? false
                                                                         :target-columns ["year"]
                                                                         :value-column-name "value"}))
pop2
loading …

Step 2 - separate "indicate" column

(def pop3 (tc/separate-column pop2
                               "indicator" ["area" "variable"]
                               #(rest (clojure.string/split % #"\."))))
pop3
loading …

Step 3 - Make columns based on "variable" values.

(tc/pivot->wider pop3 "variable" "value" {:drop-missing? false})
loading …


Multi-choice

(def multi (tc/dataset {:id [1 2 3 4]
                         :choice1 ["A" "C" "D" "B"]
                         :choice2 ["B" "B" nil "D"]
                         :choice3 ["C" nil nil nil]}))
multi
loading …

Step 1 - convert all choices into rows and add artificial column to all values which are not missing.

(def multi2 (-> multi
                (tc/pivot->longer (complement #{:id}))
                (tc/add-column :checked true)))
multi2
loading …

Step 2 - Convert back to wide form with actual choices as columns

(-> multi2
    (tc/drop-columns :$column)
    (tc/pivot->wider :$value :checked {:drop-missing? false})
    (tc/order-by :id))
loading …


Construction

(def construction (tc/dataset "data/construction.csv"))
(def construction-unit-map {"1 unit" "1"
                            "2 to 4 units" "2-4"
                            "5 units or more" "5+"})
construction
loading …

Conversion 1 - Group two column types

(-> construction
    (tc/pivot->longer #"^[125NWS].*|Midwest" {:target-columns [:units :region]
                                               :splitter (fn [col-name]
                                                           (if (re-matches #"^[125].*" col-name)
                                                             [(construction-unit-map col-name) nil]
                                                             [nil col-name]))
                                               :value-column-name :n
                                               :drop-missing? false}))
loading …

Conversion 2 - Convert to longer form and back and rename columns

(-> construction
    (tc/pivot->longer #"^[125NWS].*|Midwest" {:target-columns [:units :region]
                                               :splitter (fn [col-name]
                                                           (if (re-matches #"^[125].*" col-name)
                                                             [(construction-unit-map col-name) nil]
                                                             [nil col-name]))
                                               :value-column-name :n
                                               :drop-missing? false})
    (tc/pivot->wider [:units :region] :n {:drop-missing? false})
    (tc/rename-columns (zipmap (vals construction-unit-map)
                                (keys construction-unit-map))))
loading …

Various operations on stocks, examples taken from gather and spread manuals.

(def stocks-tidyr (tc/dataset "data/stockstidyr.csv"))
stocks-tidyr
loading …

Convert to longer form

(def stocks-long (tc/pivot->longer stocks-tidyr ["X" "Y" "Z"] {:value-column-name :price
                                                                :target-columns :stocks}))
stocks-long
loading …

Convert back to wide form

(tc/pivot->wider stocks-long :stocks :price)
loading …

Convert to wide form on time column (let’s limit values to a couple of rows)

(-> stocks-long
    (tc/select-rows (range 0 30 4))
    (tc/pivot->wider "time" :price {:drop-missing? false}))
loading …

Join/Concat Datasets

Dataset join and concatenation functions.

Joins accept left-side and right-side datasets and columns selector. Options are the same as in tech.ml.dataset functions.

A column selector can be a map with :left and :right keys to specify column names separate for left and right dataset.

The difference between tech.ml.dataset join functions are: arguments order (first datasets) and possibility to join on multiple columns.

Multiple columns joins create temporary index column from column selection. The method for creating index is based on :hashing option and defaults to identity. Prior to 7.000-beta-50 hash function was used, which caused hash collision for certain cases.

Additionally set operations are defined: intersect and difference.

To concat two datasets rowwise you can choose:

  • concat - concats rows for matching columns, the number of columns should be equal.
  • union - like concat but returns unique values
  • bind - concats rows add missing, empty columns

To add two datasets columnwise use bind. The number of rows should be equal.

Datasets used in examples:

(def ds1 (tc/dataset {:a [1 2 1 2 3 4 nil nil 4]
                       :b (range 101 110)
                       :c (map str "abs tract")}))
(def ds2 (tc/dataset {:a [nil 1 2 5 4 3 2 1 nil]
                      :b (range 110 101 -1)
                      :c (map str "datatable")
                      :d (symbol "X")
                      :e [3 4 5 6 7 nil 8 1 1]}))
ds1
loading …
ds2
loading …

Left

(tc/left-join ds1 ds2 :b)
loading …

(tc/left-join ds2 ds1 :b)
loading …

(tc/left-join ds1 ds2 [:a :b])
loading …

(tc/left-join ds2 ds1 [:a :b])
loading …

(tc/left-join ds1 ds2 {:left :a :right :e})
loading …

(tc/left-join ds2 ds1 {:left :e :right :a})
loading …

Inner

(tc/inner-join ds1 ds2 :b)
loading …

(tc/inner-join ds2 ds1 :b)
loading …

(tc/inner-join ds1 ds2 [:a :b])
loading …

(tc/inner-join ds2 ds1 [:a :b])
loading …

(tc/inner-join ds1 ds2 {:left :a :right :e})
loading …

(tc/inner-join ds2 ds1 {:left :e :right :a})
loading …

Full

Join keeping all rows

(tc/full-join ds1 ds2 :b)
loading …

(tc/full-join ds2 ds1 :b)
loading …

(tc/full-join ds1 ds2 [:a :b])
loading …

(tc/full-join ds2 ds1 [:a :b])
loading …

(tc/full-join ds1 ds2 {:left :a :right :e})
loading …

(tc/full-join ds2 ds1 {:left :e :right :a})
loading …

Semi

Return rows from ds1 matching ds2

(tc/semi-join ds1 ds2 :b)
loading …

(tc/semi-join ds2 ds1 :b)
loading …

(tc/semi-join ds1 ds2 [:a :b])
loading …

(tc/semi-join ds2 ds1 [:a :b])
loading …

(tc/semi-join ds1 ds2 {:left :a :right :e})
loading …

(tc/semi-join ds2 ds1 {:left :e :right :a})
loading …

Anti

Return rows from ds1 not matching ds2

(tc/anti-join ds1 ds2 :b)
loading …

(tc/anti-join ds2 ds1 :b)
loading …

(tc/anti-join ds1 ds2 [:a :b])
loading …

(tc/anti-join ds1 ds2 {:left :a :right :e})
loading …

(tc/anti-join ds2 ds1 {:left :e :right :a})
loading …

Hashing

When :hashing option is used, data from join columns are preprocessed by applying join-columns funtion with :result-type set to the value of :hashing. This helps to create custom joining behaviour. Function used for hashing will get vector of row values from join columns.

In the following example we will join columns on value modulo 5.

(tc/left-join ds1 ds2 :b {:hashing (fn [[v]] (mod v 5))})
loading …

Cross

Cross product from selected columns

(tc/cross-join ds1 ds2 [:a :b])
loading …

(tc/cross-join ds1 ds2 {:left [:a :b] :right :e})
loading …

Expand

Similar to cross product but works on a single dataset.

(tc/expand ds2 :a :c :d)
loading …

Columns can be also bundled (nested) in tuples which are treated as a single entity during cross product.

(tc/expand ds2 [:a :c] [:e :b])
loading …

Complete

Same as expand with all other columns preserved (filled with missing values if necessary).

(tc/complete ds2 :a :c :d)
loading …

(tc/complete ds2 [:a :c] [:e :b])
loading …

asof

(def left-ds (tc/dataset {:a [1 5 10]
                          :left-val ["a" "b" "c"]}))
(def right-ds (tc/dataset {:a [1 2 3 6 7]
                           :right-val [:a :b :c :d :e]}))
left-ds
loading …
right-ds
loading …
(tc/asof-join left-ds right-ds :a)
loading …
(tc/asof-join left-ds right-ds :a {:asof-op :nearest})
loading …
(tc/asof-join left-ds right-ds :a {:asof-op :>=})
loading …

Concat

contact joins rows from other datasets

(tc/concat ds1)
loading …

concat-copying ensures all readers are evaluated.

(tc/concat-copying ds1)
loading …

(tc/concat ds1 (tc/drop-columns ds2 :d))
loading …

^:note-to-test/skip
(apply tc/concat (repeatedly 3 #(tc/random DS)))
loading …
Concat grouped dataset

Concatenation of grouped datasets results also in grouped dataset.

(tc/concat (tc/group-by DS [:V3])
           (tc/group-by DS [:V4]))
loading …

Union

The same as concat but returns unique rows

(apply tc/union (tc/drop-columns ds2 :d) (repeat 10 ds1))
loading …

^:note-to-test/skip
(apply tc/union (repeatedly 10 #(tc/random DS)))
loading …

Bind

bind adds empty columns during concat

(tc/bind ds1 ds2)
loading …

(tc/bind ds2 ds1)
loading …

Append

append concats columns

(tc/append ds1 ds2)
loading …

Intersection

(tc/intersect (tc/select-columns ds1 :b)
              (tc/select-columns ds2 :b))
loading …

Difference

(tc/difference (tc/select-columns ds1 :b)
               (tc/select-columns ds2 :b))
loading …

(tc/difference (tc/select-columns ds2 :b)
               (tc/select-columns ds1 :b))
loading …

Split into train/test

In ML world very often you need to test given model and prepare collection of train and test datasets. split creates new dataset with two additional columns:

  • :$split-name - with :train, :test, :split-2, … values
  • :$split-id - id of splitted group (for k-fold and repeating)

split-type can be one of the following:

  • :kfold (default) - k-fold strategy, :k defines number of folds (defaults to 5), produces k splits
  • :bootstrap - :ratio defines ratio of observations put into result (defaults to 1.0), produces 1 split
  • :holdout - split into two or more parts with given ratio(s) (defaults to 2/3), produces 1 split
  • :holdouts - splits into two parts for ascending ratio. Range of rations is given by steps option
  • :loo - leave one out, produces the same number of splits as number of observations

:holdout can accept also probabilites or ratios and can split to more than 2 subdatasets

Additionally you can provide:

  • :seed - for random number generator
  • :shuffle? - turn on/off shuffle of the rows (default: true)
  • :repeats - repeat procedure :repeats times
  • :partition-selector - same as in group-by for stratified splitting to reflect dataset structure in splits.
  • :split-names names of subdatasets different than default, ie. [:train :test :split-2 ...]
  • :split-col-name - a column where name of split is stored, either :train or :test values (default: :$split-name)
  • :split-id-col-name - a column where id of the train/test pair is stored (default: :$split-id)

In case of grouped dataset each group is processed separately.

See more

^:note-to-test/skip
(def for-splitting (tc/dataset (map-indexed (fn [id v] {:id id
                                                        :partition v
                                                        :group (rand-nth [:g1 :g2 :g3])})
                                            (concat (repeat 20 :a) (repeat 5 :b)))))
^:note-to-test/skip
for-splitting
loading …

k-Fold

Returns k=5 maps

^:note-to-test/skip
(-> for-splitting
    (tc/split)
    (tc/head 30))
loading …

Partition according to :k column to reflect it’s distribution

^:note-to-test/skip
(-> for-splitting
    (tc/split :kfold {:partition-selector :partition})
    (tc/head 30))
loading …

Bootstrap

^:note-to-test/skip
(tc/split for-splitting :bootstrap)
loading …

with repeats, to get 100 splits

^:note-to-test/skip
(-> for-splitting
    (tc/split :bootstrap {:repeats 100})
    (:$split-id)
    (distinct)
    (count))
100

Holdout

with small ratio

^:note-to-test/skip
(tc/split for-splitting :holdout {:ratio 0.2})
loading …

you can split to more than two subdatasets with holdout

^:note-to-test/skip
(tc/split for-splitting :holdout {:ratio [0.1 0.2 0.3 0.15 0.25]})
loading …

you can use also proportions with custom names

^:note-to-test/skip
(tc/split for-splitting :holdout {:ratio [5 3 11 2]
                                  :split-names ["small" "smaller" "big" "the rest"]})
loading …

Holdouts

With ratios from 5% to 95% of the dataset with step 1.5 generates 15 splits with ascending rows in train dataset.

^:note-to-test/skip
(-> (tc/split for-splitting :holdouts {:steps [0.05 0.95 1.5]
                                       :shuffle? false})
    (tc/group-by [:$split-id :$split-name]))
loading …

Leave One Out

^:note-to-test/skip
(-> for-splitting
    (tc/split :loo)
    (tc/head 30))
loading …
^:note-to-test/skip
(-> for-splitting
    (tc/split :loo)
    (tc/row-count))
625

Grouped dataset with partitioning

^:note-to-test/skip
(-> for-splitting
    (tc/group-by :group)
    (tc/split :bootstrap {:partition-selector :partition :seed 11 :ratio 0.8}))
loading …

Split as a sequence

To get a sequence of pairs, use split->seq function

^:note-to-test/skip
(-> for-splitting
    (tc/split->seq :kfold {:partition-selector :partition})
    (first))
loading …
^:note-to-test/skip
(-> for-splitting
    (tc/group-by :group)
    (tc/split->seq :bootstrap {:partition-selector :partition :seed 11 :ratio 0.8 :repeats 2})
    (first))
loading …

Pipeline

tablecloth.pipeline exports special versions of API which create functions operating only on dataset. This creates the possibility to chain operations and compose them easily.

There are two ways to create pipelines:

  • functional, as a composition of functions
  • declarative, separating task declarations and concrete parametrization.

Pipeline operations are prepared to work with metamorph library. That means that result of the pipeline is wrapped into a map and dataset is stored under :metamorph/data key.

Warning: Duplicated metamorph pipeline functions are removed from tablecloth.pipeline namespace.

Functions

This API doesn’t provide any statistical, numerical or date/time functions. Use below namespaces:

Namespace functions
tech.v3.datatype.functional primitive oprations, reducers, statistics
tech.v3.datatype.datetime date/time converters and operations

Other examples

Stocks

(defonce stocks (tc/dataset "https://raw.githubusercontent.com/techascent/tech.ml.dataset/master/test/data/stocks.csv" {:key-fn keyword}))
nil
stocks
loading …
(-> stocks
    (tc/group-by (fn [row]
                    {:symbol (:symbol row)
                     :year (tech.v3.datatype.datetime/long-temporal-field :years (:date row))}))
    (tc/aggregate #(tech.v3.datatype.functional/mean (% :price)))
    (tc/order-by [:symbol :year]))
loading …
(-> stocks
    (tc/group-by (juxt :symbol #(tech.v3.datatype.datetime/long-temporal-field :years (% :date))))
    (tc/aggregate #(tech.v3.datatype.functional/mean (% :price)))
    (tc/rename-columns {:$group-name-0 :symbol
                         :$group-name-1 :year}))
loading …

data.table

Below you can find comparizon between functionality of data.table and Clojure dataset API. I leave it without comments, please refer original document explaining details:

Introduction to data.table

R

library(data.table)
library(knitr)

flights <- fread("https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv")

kable(head(flights))
year month day dep_delay arr_delay carrier origin dest air_time distance hour
2014 1 1 14 13 AA JFK LAX 359 2475 9
2014 1 1 -3 13 AA JFK LAX 363 2475 11
2014 1 1 2 9 AA JFK LAX 351 2475 19
2014 1 1 -8 -26 AA LGA PBI 157 1035 7
2014 1 1 2 1 AA JFK LAX 350 2475 13
2014 1 1 4 0 AA EWR LAX 339 2454 18

Clojure

(require '[tech.v3.datatype.functional :as dfn]
         '[tech.v3.datatype.argops :as aops]
         '[tech.v3.datatype :as dtype])
nil
(defonce flights (tc/dataset "https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv"))
nil
(tc/head flights 6)
loading …

Basics

Shape of loaded data

R

dim(flights)
[1] 253316     11

Clojure

(tc/shape flights)
[253316 11]
What is data.table?

R

DT = data.table(
  ID = c("b","b","b","a","a","c"),
  a = 1:6,
  b = 7:12,
  c = 13:18
)

kable(DT)
ID a b c
b 1 7 13
b 2 8 14
b 3 9 15
a 4 10 16
a 5 11 17
c 6 12 18
class(DT$ID)
[1] "character"

Clojure

(def DT (tc/dataset {:ID ["b" "b" "b" "a" "a" "c"]
                      :a (range 1 7)
                      :b (range 7 13)
                      :c (range 13 19)}))
DT
loading …
(-> :ID DT meta :datatype)
:string
Get all the flights with “JFK” as the origin airport in the month of June.

R

ans <- flights[origin == "JFK" & month == 6L]
kable(head(ans))
year month day dep_delay arr_delay carrier origin dest air_time distance hour
2014 6 1 -9 -5 AA JFK LAX 324 2475 8
2014 6 1 -10 -13 AA JFK LAX 329 2475 12
2014 6 1 18 -1 AA JFK LAX 326 2475 7
2014 6 1 -6 -16 AA JFK LAX 320 2475 10
2014 6 1 -4 -45 AA JFK LAX 326 2475 18
2014 6 1 -6 -23 AA JFK LAX 329 2475 14

Clojure

(-> flights
    (tc/select-rows (fn [row] (and (= (get row "origin") "JFK")
                                   (= (get row "month") 6))))
    (tc/head 6))
loading …
Get the first two rows from flights.

R

ans <- flights[1:2]
kable(ans)
year month day dep_delay arr_delay carrier origin dest air_time distance hour
2014 1 1 14 13 AA JFK LAX 359 2475 9
2014 1 1 -3 13 AA JFK LAX 363 2475 11

Clojure

(tc/select-rows flights (range 2))
loading …
Sort flights first by column origin in ascending order, and then by dest in descending order

R

ans <- flights[order(origin, -dest)]
kable(head(ans))
year month day dep_delay arr_delay carrier origin dest air_time distance hour
2014 1 5 6 49 EV EWR XNA 195 1131 8
2014 1 6 7 13 EV EWR XNA 190 1131 8
2014 1 7 -6 -13 EV EWR XNA 179 1131 8
2014 1 8 -7 -12 EV EWR XNA 184 1131 8
2014 1 9 16 7 EV EWR XNA 181 1131 8
2014 1 13 66 66 EV EWR XNA 188 1131 9

Clojure

(-> flights
    (tc/order-by ["origin" "dest"] [:asc :desc])
    (tc/head 6))
loading …
Select arr_delay column, but return it as a vector

R

ans <- flights[, arr_delay]
head(ans)
[1]  13  13   9 -26   1   0

Clojure

(take 6 (flights "arr_delay"))
(13 13 9 -26 1 0)
Select arr_delay column, but return as a data.table instead

R

ans <- flights[, list(arr_delay)]
kable(head(ans))
arr_delay
13
13
9
-26
1
0

Clojure

(-> flights
    (tc/select-columns "arr_delay")
    (tc/head 6))
loading …
Select both arr_delay and dep_delay columns

R

ans <- flights[, .(arr_delay, dep_delay)]
kable(head(ans))
arr_delay dep_delay
13 14
13 -3
9 2
-26 -8
1 2
0 4

Clojure

(-> flights
    (tc/select-columns ["arr_delay" "dep_delay"])
    (tc/head 6))
loading …
Select both arr_delay and dep_delay columns and rename them to delay_arr and delay_dep

R

ans <- flights[, .(delay_arr = arr_delay, delay_dep = dep_delay)]
kable(head(ans))
delay_arr delay_dep
13 14
13 -3
9 2
-26 -8
1 2
0 4

Clojure

(-> flights
    (tc/select-columns {"arr_delay" "delay_arr"
                         "dep_delay" "delay_arr"})
    (tc/head 6))
loading …
How many trips have had total delay < 0?

R

ans <- flights[, sum( (arr_delay + dep_delay) < 0 )]
ans
[1] 141814

Clojure

(->> (dfn/+ (flights "arr_delay") (flights "dep_delay"))
     (aops/argfilter #(< % 0.0))
     (dtype/ecount))
141814

or pure Clojure functions (much, much slower)

(->> (map + (flights "arr_delay") (flights "dep_delay"))
     (filter neg?)
     (count))
141814
Calculate the average arrival and departure delay for all flights with “JFK” as the origin airport in the month of June

R

ans <- flights[origin == "JFK" & month == 6L,
               .(m_arr = mean(arr_delay), m_dep = mean(dep_delay))]
kable(ans)
m_arr m_dep
5.839349 9.807884

Clojure

(-> flights
    (tc/select-rows (fn [row] (and (= (get row "origin") "JFK")
                                   (= (get row "month") 6))))
    (tc/aggregate {:m_arr #(dfn/mean (% "arr_delay"))
                    :m_dep #(dfn/mean (% "dep_delay"))}))
loading …
How many trips have been made in 2014 from “JFK” airport in the month of June?

R

ans <- flights[origin == "JFK" & month == 6L, length(dest)]
ans
[1] 8422

or

ans <- flights[origin == "JFK" & month == 6L, .N]
ans
[1] 8422

Clojure

(-> flights
    (tc/select-rows (fn [row] (and (= (get row "origin") "JFK")
                                   (= (get row "month") 6))))
    (tc/row-count))
8422
deselect columns using - or !

R

ans <- flights[, !c("arr_delay", "dep_delay")]
kable(head(ans))
year month day carrier origin dest air_time distance hour
2014 1 1 AA JFK LAX 359 2475 9
2014 1 1 AA JFK LAX 363 2475 11
2014 1 1 AA JFK LAX 351 2475 19
2014 1 1 AA LGA PBI 157 1035 7
2014 1 1 AA JFK LAX 350 2475 13
2014 1 1 AA EWR LAX 339 2454 18

or

ans <- flights[, -c("arr_delay", "dep_delay")]
kable(head(ans))
year month day carrier origin dest air_time distance hour
2014 1 1 AA JFK LAX 359 2475 9
2014 1 1 AA JFK LAX 363 2475 11
2014 1 1 AA JFK LAX 351 2475 19
2014 1 1 AA LGA PBI 157 1035 7
2014 1 1 AA JFK LAX 350 2475 13
2014 1 1 AA EWR LAX 339 2454 18

Clojure

(-> flights
    (tc/select-columns (complement #{"arr_delay" "dep_delay"}))
    (tc/head 6))
loading …

Aggregations

How can we get the number of trips corresponding to each origin airport?

R

ans <- flights[, .(.N), by = .(origin)]
kable(ans)
origin N
JFK 81483
LGA 84433
EWR 87400

Clojure

(-> flights
    (tc/group-by ["origin"])
    (tc/aggregate {:N tc/row-count}))
loading …
How can we calculate the number of trips for each origin airport for carrier code “AA”?

R

ans <- flights[carrier == "AA", .N, by = origin]
kable(ans)
origin N
JFK 11923
LGA 11730
EWR 2649

Clojure

(-> flights
    (tc/select-rows #(= (get % "carrier") "AA"))
    (tc/group-by ["origin"])
    (tc/aggregate {:N tc/row-count}))
loading …
How can we get the total number of trips for each origin, dest pair for carrier code “AA”?

R

ans <- flights[carrier == "AA", .N, by = .(origin, dest)]
kable(head(ans))
origin dest N
JFK LAX 3387
LGA PBI 245
EWR LAX 62
JFK MIA 1876
JFK SEA 298
EWR MIA 848

Clojure

(-> flights
    (tc/select-rows #(= (get % "carrier") "AA"))
    (tc/group-by ["origin" "dest"])
    (tc/aggregate {:N tc/row-count})
    (tc/head 6))
loading …
How can we get the average arrival and departure delay for each orig,dest pair for each month for carrier code “AA”?

R

ans <- flights[carrier == "AA",
        .(mean(arr_delay), mean(dep_delay)),
        by = .(origin, dest, month)]
kable(head(ans,10))
origin dest month V1 V2
JFK LAX 1 6.590361 14.2289157
LGA PBI 1 -7.758621 0.3103448
EWR LAX 1 1.366667 7.5000000
JFK MIA 1 15.720670 18.7430168
JFK SEA 1 14.357143 30.7500000
EWR MIA 1 11.011236 12.1235955
JFK SFO 1 19.252252 28.6396396
JFK BOS 1 12.919643 15.2142857
JFK ORD 1 31.586207 40.1724138
JFK IAH 1 28.857143 14.2857143

Clojure

(-> flights
    (tc/select-rows #(= (get % "carrier") "AA"))
    (tc/group-by ["origin" "dest" "month"])
    (tc/aggregate [#(dfn/mean (% "arr_delay"))
                    #(dfn/mean (% "dep_delay"))])
    (tc/head 10))
loading …
So how can we directly order by all the grouping variables?

R

ans <- flights[carrier == "AA",
        .(mean(arr_delay), mean(dep_delay)),
        keyby = .(origin, dest, month)]
kable(head(ans,10))
origin dest month V1 V2
EWR DFW 1 6.427673 10.012579
EWR DFW 2 10.536765 11.345588
EWR DFW 3 12.865031 8.079755
EWR DFW 4 17.792683 12.920732
EWR DFW 5 18.487805 18.682927
EWR DFW 6 37.005952 38.744048
EWR DFW 7 20.250000 21.154762
EWR DFW 8 16.936046 22.069767
EWR DFW 9 5.865031 13.055215
EWR DFW 10 18.813665 18.894410

Clojure

(-> flights
    (tc/select-rows #(= (get % "carrier") "AA"))
    (tc/group-by ["origin" "dest" "month"])
    (tc/aggregate [#(dfn/mean (% "arr_delay"))
                    #(dfn/mean (% "dep_delay"))])
    (tc/order-by ["origin" "dest" "month"])
    (tc/head 10))
loading …
Can by accept expressions as well or does it just take columns?

R

ans <- flights[, .N, .(dep_delay>0, arr_delay>0)]
kable(ans)
dep_delay arr_delay N
TRUE TRUE 72836
FALSE TRUE 34583
FALSE FALSE 119304
TRUE FALSE 26593

Clojure

(-> flights
    (tc/group-by (fn [row]
                    {:dep_delay (pos? (get row "dep_delay"))
                     :arr_delay (pos? (get row "arr_delay"))}))
    (tc/aggregate {:N tc/row-count}))
loading …
Do we have to compute mean() for each column individually?

R

kable(DT)
ID a b c
b 1 7 13
b 2 8 14
b 3 9 15
a 4 10 16
a 5 11 17
c 6 12 18
DT[, print(.SD), by = ID]
   a b  c
1: 1 7 13
2: 2 8 14
3: 3 9 15
   a  b  c
1: 4 10 16
2: 5 11 17
   a  b  c
1: 6 12 18
Empty data.table (0 rows and 1 cols): ID
kable(DT[, lapply(.SD, mean), by = ID])
ID a b c
b 2.0 8.0 14.0
a 4.5 10.5 16.5
c 6.0 12.0 18.0

Clojure

DT
loading …
(tc/group-by DT :ID {:result-type :as-map})
loading …
(-> DT
    (tc/group-by [:ID])
    (tc/aggregate-columns (complement #{:ID}) dfn/mean))
loading …
How can we specify just the columns we would like to compute the mean() on?

R

kable(head(flights[carrier == "AA",                         ## Only on trips with carrier "AA"
                   lapply(.SD, mean),                       ## compute the mean
                   by = .(origin, dest, month),             ## for every 'origin,dest,month'
                   .SDcols = c("arr_delay", "dep_delay")])) ## for just those specified in .SDcols
origin dest month arr_delay dep_delay
JFK LAX 1 6.590361 14.2289157
LGA PBI 1 -7.758621 0.3103448
EWR LAX 1 1.366667 7.5000000
JFK MIA 1 15.720670 18.7430168
JFK SEA 1 14.357143 30.7500000
EWR MIA 1 11.011236 12.1235955

Clojure

(-> flights
    (tc/select-rows #(= (get % "carrier") "AA"))
    (tc/group-by ["origin" "dest" "month"])
    (tc/aggregate-columns ["arr_delay" "dep_delay"] dfn/mean)
    (tc/head 6))
loading …
How can we return the first two rows for each month?

R

ans <- flights[, head(.SD, 2), by = month]
kable(head(ans))
month year day dep_delay arr_delay carrier origin dest air_time distance hour
1 2014 1 14 13 AA JFK LAX 359 2475 9
1 2014 1 -3 13 AA JFK LAX 363 2475 11
2 2014 1 -1 1 AA JFK LAX 358 2475 8
2 2014 1 -5 3 AA JFK LAX 358 2475 11
3 2014 1 -11 36 AA JFK LAX 375 2475 8
3 2014 1 -3 14 AA JFK LAX 368 2475 11

Clojure

(-> flights
    (tc/group-by ["month"])
    (tc/head 2) ;; head applied on each group
    (tc/ungroup)
    (tc/head 6))
loading …
How can we concatenate columns a and b for each group in ID?

R

kable(DT[, .(val = c(a,b)), by = ID])
ID val
b 1
b 2
b 3
b 7
b 8
b 9
a 4
a 5
a 10
a 11
c 6
c 12

Clojure

(-> DT
    (tc/pivot->longer [:a :b] {:value-column-name :val})
    (tc/drop-columns [:$column :c]))
loading …
What if we would like to have all the values of column a and b concatenated, but returned as a list column?

R

kable(DT[, .(val = list(c(a,b))), by = ID])
ID val
b 1, 2, 3, 7, 8, 9
a 4, 5, 10, 11
c 6, 12

Clojure

(-> DT
    (tc/pivot->longer [:a :b] {:value-column-name :val})
    (tc/drop-columns [:$column :c])
    (tc/fold-by :ID))
loading …

API tour

Below snippets are taken from A data.table and dplyr tour written by Atrebas (permission granted).

I keep structure and subtitles but I skip data.table and dplyr examples.

Example data

(def DS (tc/dataset {:V1 (take 9 (cycle [1 2]))
                      :V2 (range 1 10)
                      :V3 (take 9 (cycle [0.5 1.0 1.5]))
                      :V4 (take 9 (cycle ["A" "B" "C"]))}))
(tc/dataset? DS)
true
(class DS)
tech.v3.dataset.impl.dataset.Dataset
DS
loading …

Basic Operations

Filter rows

Filter rows using indices

(tc/select-rows DS [2 3])
loading …

Discard rows using negative indices

In Clojure API we have separate function for that: drop-rows.

(tc/drop-rows DS (range 2 7))
loading …

Filter rows using a logical expression

(tc/select-rows DS (comp #(> % 5) :V2))
loading …
(tc/select-rows DS (comp #{"A" "C"} :V4))
loading …

Filter rows using multiple conditions

(tc/select-rows DS #(and (= (:V1 %) 1)
                          (= (:V4 %) "A")))
loading …

Filter unique rows

(tc/unique-by DS)
loading …
(tc/unique-by DS [:V1 :V4])
loading …

Discard rows with missing values

(tc/drop-missing DS)
loading …

Other filters

^:note-to-test/skip
(tc/random DS 3)
loading …

3 random rows

^:note-to-test/skip
(tc/random DS (/ (tc/row-count DS) 2))
loading …

fraction of random rows

(tc/by-rank DS :V1 zero?)
loading …

take top n entries


Convenience functions

(tc/select-rows DS (comp (partial re-matches #"^B") str :V4))
loading …
(tc/select-rows DS (comp #(<= 3 % 5) :V2))
loading …
(tc/select-rows DS (comp #(< 3 % 5) :V2))
loading …
(tc/select-rows DS (comp #(<= 3 % 5) :V2))
loading …

Last example skipped.

Sort rows

Sort rows by column

(tc/order-by DS :V3)
loading …

Sort rows in decreasing order

(tc/order-by DS :V3 :desc)
loading …

Sort rows based on several columns

(tc/order-by DS [:V1 :V2] [:asc :desc])
loading …
Select columns

Select one column using an index (not recommended)

(nth (tc/columns DS :as-seq) 2)
#tech.v3.dataset.column&lt;float64&gt;[9]
:V3
[0.5000, 1.000, 1.500, 0.5000, 1.000, 1.500, 0.5000, 1.000, 1.500]

as column (iterable)

(tc/dataset [(nth (tc/columns DS :as-seq) 2)])
loading …

Select one column using column name

(tc/select-columns DS :V2)
loading …

as dataset

(tc/select-columns DS [:V2])
loading …

as dataset

(DS :V2)
#tech.v3.dataset.column&lt;int64&gt;[9]
:V2
[1, 2, 3, 4, 5, 6, 7, 8, 9]

as column (iterable)


Select several columns

(tc/select-columns DS [:V2 :V3 :V4])
loading …

Exclude columns

(tc/select-columns DS (complement #{:V2 :V3 :V4}))
loading …
(tc/drop-columns DS [:V2 :V3 :V4])
loading …

Other seletions

(->> (range 1 3)
     (map (comp keyword (partial format "V%d")))
     (tc/select-columns DS))
loading …
(tc/reorder-columns DS :V4)
loading …
(tc/select-columns DS #(clojure.string/starts-with? (name %) "V"))
loading …
(tc/select-columns DS #(clojure.string/ends-with? (name %) "3"))
loading …
(tc/select-columns DS #"..2")
loading …

regex converts to string using str function

(tc/select-columns DS #{:V1 "X"})
loading …
(tc/select-columns DS #(not (clojure.string/starts-with? (name %) "V2")))
loading …
Summarise data

Summarise one column

(reduce + (DS :V1))
13

using pure Clojure, as value

(tc/aggregate-columns DS :V1 dfn/sum)
loading …

as dataset

(tc/aggregate DS {:sumV1 #(dfn/sum (% :V1))})
loading …

Summarize several columns

(tc/aggregate DS [#(dfn/sum (% :V1))
                   #(dfn/standard-deviation (% :V3))])
loading …
(tc/aggregate-columns DS [:V1 :V3] [dfn/sum
                                     dfn/standard-deviation])
loading …

Summarise several columns and assign column names

(tc/aggregate DS {:sumv1 #(dfn/sum (% :V1))
                   :sdv3 #(dfn/standard-deviation (% :V3))})
loading …

Summarise a subset of rows

(-> DS
    (tc/select-rows (range 4))
    (tc/aggregate-columns :V1 dfn/sum))
loading …
Additional helpers
(-> DS
    (tc/first)
    (tc/select-columns :V3))
loading …

select first row from :V3 column

(-> DS
    (tc/last)
    (tc/select-columns :V3))
loading …

select last row from :V3 column

(-> DS
    (tc/select-rows 4)
    (tc/select-columns :V3))
loading …

select forth row from :V3 column

(-> DS
    (tc/select :V3 4))
loading …

select forth row from :V3 column

(-> DS
    (tc/unique-by :V4)
    (tc/aggregate tc/row-count))
loading …

number of unique rows in :V4 column, as dataset

(-> DS
    (tc/unique-by :V4)
    (tc/row-count))
3

number of unique rows in :V4 column, as value

(-> DS
    (tc/unique-by)
    (tc/row-count))
9

number of unique rows in dataset, as value

Add/update/delete columns

Modify a column

(tc/map-columns DS :V1 [:V1] #(dfn/pow % 2))
loading …
(def DS (tc/add-column DS :V1 (dfn/pow (DS :V1) 2)))
DS
loading …

Add one column

(tc/map-columns DS :v5 [:V1] dfn/log)
loading …
(def DS (tc/add-column DS :v5 (dfn/log (DS :V1))))
DS
loading …

Add several columns

(def DS (tc/add-columns DS {:v6 (dfn/sqrt (DS :V1))
                                       :v7 "X"}))
DS
loading …

Create one column and remove the others

(tc/dataset {:v8 (dfn/+ (DS :V3) 1)})
loading …

Remove one column

(def DS (tc/drop-columns DS :v5))
DS
loading …

Remove several columns

(def DS (tc/drop-columns DS [:v6 :v7]))
DS
loading …

Remove columns using a vector of colnames

We use set here.

(def DS (tc/select-columns DS (complement #{:V3})))
DS
loading …

Replace values for rows matching a condition

(def DS (tc/map-columns DS :V2 [:V2] #(if (< % 4.0) 0.0 %)))
DS
loading …
by

By group

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate {:sumV2 #(dfn/sum (% :V2))}))
loading …

By several groups

(-> DS
    (tc/group-by [:V4 :V1])
    (tc/aggregate {:sumV2 #(dfn/sum (% :V2))}))
loading …

Calling function in by

(-> DS
    (tc/group-by (fn [row]
                    (clojure.string/lower-case (:V4 row))))
    (tc/aggregate {:sumV1 #(dfn/sum (% :V1))}))
loading …

Assigning column name in by

(-> DS
    (tc/group-by (fn [row]
                    {:abc (clojure.string/lower-case (:V4 row))}))
    (tc/aggregate {:sumV1 #(dfn/sum (% :V1))}))
loading …
(-> DS
    (tc/group-by (fn [row]
                    (clojure.string/lower-case (:V4 row))))
    (tc/aggregate {:sumV1 #(dfn/sum (% :V1))} {:add-group-as-column :abc}))
loading …

Using a condition in by

(-> DS
    (tc/group-by #(= (:V4 %) "A"))
    (tc/aggregate #(dfn/sum (% :V1))))
loading …

By on a subset of rows

(-> DS
    (tc/select-rows (range 5))
    (tc/group-by :V4)
    (tc/aggregate {:sumV1 #(dfn/sum (% :V1))}))
loading …

Count number of observations for each group

(-> DS
    (tc/group-by :V4)
    (tc/aggregate tc/row-count))
loading …

Add a column with number of observations for each group

(-> DS
    (tc/group-by [:V1])
    (tc/add-column :n tc/row-count)
    (tc/ungroup))
loading …

Retrieve the first/last/nth observation for each group

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate-columns :V2 first))
loading …
(-> DS
    (tc/group-by [:V4])
    (tc/aggregate-columns :V2 last))
loading …
(-> DS
    (tc/group-by [:V4])
    (tc/aggregate-columns :V2 #(nth % 1)))
loading …

Going further

Advanced columns manipulation

Summarise all the columns

custom max function which works on every type

(tc/aggregate-columns DS :all (fn [col] (first (sort #(compare %2 %1) col))))
loading …

Summarise several columns

(tc/aggregate-columns DS [:V1 :V2] dfn/mean)
loading …

Summarise several columns by group

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate-columns [:V1 :V2] dfn/mean))
loading …

Summarise with more than one function by group

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate-columns [:V1 :V2] (fn [col]
                                       {:sum (dfn/sum col)
                                        :mean (dfn/mean col)})))
loading …

Summarise using a condition

(-> DS
    (tc/select-columns :type/numerical)
    (tc/aggregate-columns :all dfn/mean))
loading …

Modify all the columns

(tc/update-columns DS :all reverse)
loading …

Modify several columns (dropping the others)

(-> DS
    (tc/select-columns [:V1 :V2])
    (tc/update-columns :all dfn/sqrt))
loading …
(-> DS
    (tc/select-columns (complement #{:V4}))
    (tc/update-columns :all dfn/exp))
loading …

Modify several columns (keeping the others)

(def DS (tc/update-columns DS [:V1 :V2] dfn/sqrt))
DS
loading …
(def DS (tc/update-columns DS (complement #{:V4}) #(dfn/pow % 2)))
DS
loading …

Modify columns using a condition (dropping the others)

(-> DS
    (tc/select-columns :type/numerical)
    (tc/update-columns :all #(dfn/- % 1)))
loading …

Modify columns using a condition (keeping the others)

(def DS (tc/convert-types DS :type/numerical :int32))
DS
loading …

Use a complex expression

(-> DS
    (tc/group-by [:V4])
    (tc/head 2)
    (tc/add-column :V2 "X")
    (tc/ungroup))
loading …

Use multiple expressions

(tc/dataset (let [x (dfn/+ (DS :V1) (dfn/sum (DS :V2)))]
               (println (seq (DS :V1)))
               (println (tc/info (tc/select-columns DS :V1)))
               {:A (range 1 (inc (tc/row-count DS)))
                :B x}))
loading …
Chain expressions

Expression chaining using >

(-> DS
    (tc/group-by [:V4])
    (tc/aggregate {:V1sum #(dfn/sum (% :V1))})
    (tc/select-rows #(>= (:V1sum %) 5)))
loading …
(-> DS
    (tc/group-by [:V4])
    (tc/aggregate {:V1sum #(dfn/sum (% :V1))})
    (tc/order-by :V1sum :desc))
loading …
Indexing and Keys

Set the key/index (order)

(def DS (tc/order-by DS :V4))
DS
loading …

Select the matching rows

(tc/select-rows DS #(= (:V4 %) "A"))
loading …
(tc/select-rows DS (comp #{"A" "C"} :V4))
loading …

Select the first matching row

(-> DS
    (tc/select-rows #(= (:V4 %) "B"))
    (tc/first))
loading …
(-> DS
    (tc/unique-by :V4)
    (tc/select-rows (comp #{"B" "C"} :V4)))
loading …

Select the last matching row

(-> DS
    (tc/select-rows #(= (:V4 %) "A"))
    (tc/last))
loading …

Nomatch argument

(tc/select-rows DS (comp #{"A" "D"} :V4))
loading …

Apply a function on the matching rows

(-> DS
    (tc/select-rows (comp #{"A" "C"} :V4))
    (tc/aggregate-columns :V1 (fn [col]
                                 {:sum (dfn/sum col)})))
loading …

Modify values for matching rows

(def DS (-> DS
            (tc/map-columns :V1 [:V1 :V4] #(if (= %2 "A") 0 %1))
            (tc/order-by :V4)))
DS
loading …

Use keys in by

(-> DS
    (tc/select-rows (comp (complement #{"B"}) :V4))
    (tc/group-by [:V4])
    (tc/aggregate-columns :V1 dfn/sum))
loading …

Set keys/indices for multiple columns (ordered)

(tc/order-by DS [:V4 :V1])
loading …

Subset using multiple keys/indices

(-> DS
    (tc/select-rows #(and (= (:V1 %) 1)
                           (= (:V4 %) "C"))))
loading …
(-> DS
    (tc/select-rows #(and (= (:V1 %) 1)
                           (#{"B" "C"} (:V4 %)))))
loading …
(-> DS
    (tc/select-rows #(and (= (:V1 %) 1)
                           (#{"B" "C"} (:V4 %))) {:result-type :as-indexes}))
(4 6 8)
set*() modifications

Replace values

There is no mutating operations tech.ml.dataset or easy way to set value.

(def DS (tc/update-columns DS :V2 #(map-indexed (fn [idx v]
                                                   (if (zero? idx) 3 v)) %)))
DS
loading …

Reorder rows

(def DS (tc/order-by DS [:V4 :V1] [:asc :desc]))
DS
loading …

Modify colnames

(def DS (tc/rename-columns DS {:V2 "v2"}))
DS
loading …
(def DS (tc/rename-columns DS {"v2" :V2}))

revert back


Reorder columns

(def DS (tc/reorder-columns DS :V4 :V1 :V2))
DS
loading …
Advanced use of by

Select first/last/… row by group

(-> DS
    (tc/group-by :V4)
    (tc/first)
    (tc/ungroup))
loading …
(-> DS
    (tc/group-by :V4)
    (tc/select-rows [0 2])
    (tc/ungroup))
loading …
(-> DS
    (tc/group-by :V4)
    (tc/tail 2)
    (tc/ungroup))
loading …

Select rows using a nested query

(-> DS
    (tc/group-by :V4)
    (tc/order-by :V2)
    (tc/first)
    (tc/ungroup))
loading …

Add a group counter column

(-> DS
    (tc/group-by [:V4 :V1])
    (tc/ungroup {:add-group-id-as-column :Grp}))
loading …

Get row number of first (and last) observation by group

(-> DS
    (tc/add-column :row-id (range))
    (tc/select-columns [:V4 :row-id])
    (tc/group-by :V4)
    (tc/ungroup))
loading …
(-> DS
    (tc/add-column :row-id (range))
    (tc/select-columns [:V4 :row-id])
    (tc/group-by :V4)
    (tc/first)
    (tc/ungroup))
loading …
(-> DS
    (tc/add-column :row-id (range))
    (tc/select-columns [:V4 :row-id])
    (tc/group-by :V4)
    (tc/select-rows [0 2])
    (tc/ungroup))
loading …

Handle list-columns by group

(-> DS
    (tc/select-columns [:V1 :V4])
    (tc/fold-by :V4))
loading …
(-> DS
    (tc/group-by :V4)
    (tc/unmark-group))
loading …

Grouping sets (multiple by at once)

Not available.

Miscellaneous

Read / Write data

Write data to a csv file

(tc/write! DS "DF.csv")
nil

Write data to a tab-delimited file

(tc/write! DS "DF.txt" {:separator \tab})
nil

or

(tc/write! DS "DF.tsv")
nil

Read a csv / tab-delimited file

(tc/dataset "DF.csv" {:key-fn keyword})
loading …
^:note-to-test/skip
(tc/dataset "DF.txt" {:key-fn keyword})
loading …
(tc/dataset "DF.tsv" {:key-fn keyword})
loading …

Read a csv file selecting / droping columns

(tc/dataset "DF.csv" {:key-fn keyword
                       :column-whitelist ["V1" "V4"]})
loading …
(tc/dataset "DF.csv" {:key-fn keyword
                       :column-blacklist ["V4"]})
loading …

Read and rbind several files

(apply tc/concat (map tc/dataset ["DF.csv" "DF.csv"]))
loading …
Reshape data

Melt data (from wide to long)

(def mDS (tc/pivot->longer DS [:V1 :V2] {:target-columns :variable
                                          :value-column-name :value}))
mDS
loading …

Cast data (from long to wide)

(-> mDS
    (tc/pivot->wider :variable :value {:fold-fn vec})
    (tc/update-columns ["V1" "V2"] (partial map count)))
loading …
(-> mDS
    (tc/pivot->wider :variable :value {:fold-fn vec})
    (tc/update-columns ["V1" "V2"] (partial map dfn/sum)))
loading …
(-> mDS
    (tc/map-columns :value #(str (> % 5))) ;; coerce to strings
    (tc/pivot->wider :value :variable {:fold-fn vec})
    (tc/update-columns ["true" "false"] (partial map #(if (sequential? %) (count %) 1))))
loading …

Split

(tc/group-by DS :V4 {:result-type :as-map})
loading …

Split and transpose a vector/column

(-> {:a ["A:a" "B:b" "C:c"]}
    (tc/dataset)
    (tc/separate-column :a [:V1 :V2] ":"))
loading …
Other

Skipped

Join/Bind data sets

(def x (tc/dataset {"Id" ["A" "B" "C" "C"]
                     "X1" [1 3 5 7]
                     "XY" ["x2" "x4" "x6" "x8"]}))
(def y (tc/dataset {"Id" ["A" "B" "B" "D"]
                     "Y1" [1 3 5 7]
                     "XY" ["y1" "y3" "y5" "y7"]}))
x
loading …
y
loading …
Join

Join matching rows from y to x

(tc/left-join x y "Id")
loading …

Join matching rows from x to y

(tc/right-join x y "Id")
loading …

Join matching rows from both x and y

(tc/inner-join x y "Id")
loading …

Join keeping all the rows

(tc/full-join x y "Id")
loading …

Return rows from x matching y

(tc/semi-join x y "Id")
loading …

Return rows from x not matching y

(tc/anti-join x y "Id")
loading …
More joins

Select columns while joining

(tc/right-join (tc/select-columns x ["Id" "X1"])
                (tc/select-columns y ["Id" "XY"])
                "Id")
loading …
(tc/right-join (tc/select-columns x ["Id" "XY"])
                (tc/select-columns y ["Id" "XY"])
                "Id")
loading …

Aggregate columns while joining

(-> y
    (tc/group-by ["Id"])
    (tc/aggregate {"sumY1" #(dfn/sum (% "Y1"))})
    (tc/right-join x "Id")
    (tc/add-column "X1Y1" (fn [ds] (dfn/* (ds "sumY1")
                                                    (ds "X1"))))
    (tc/select-columns ["right.Id" "X1Y1"]))
loading …

Update columns while joining

(-> x
    (tc/select-columns ["Id" "X1"])
    (tc/map-columns "SqX1" "X1" (fn [x] (* x x)))
    (tc/right-join y "Id")
    (tc/drop-columns ["X1" "Id"]))
loading …

Adds a list column with rows from y matching x (nest-join)

(-> (tc/left-join x y "Id")
    (tc/drop-columns ["right.Id"])
    (tc/fold-by (tc/column-names x)))
loading …

Some joins are skipped


Cross join

(def cjds (tc/dataset {:V1 [[2 1 1]]
                        :V2 [[3 2]]}))
cjds
loading …
(reduce #(tc/unroll %1 %2) cjds (tc/column-names cjds))
loading …
(-> (reduce #(tc/unroll %1 %2) cjds (tc/column-names cjds))
    (tc/unique-by))
loading …
Bind
(def x (tc/dataset {:V1 [1 2 3]}))
(def y (tc/dataset {:V1 [4 5 6]}))
(def z (tc/dataset {:V1 [7 8 9]
                    :V2 [0 0 0]}))
x
loading …
y
loading …
z
loading …

Bind rows

(tc/bind x y)
loading …
(tc/bind x z)
loading …

Bind rows using a list

(->> [x y]
     (map-indexed #(tc/add-column %2 :id (repeat %1)))
     (apply tc/bind))
loading …

Bind columns

(tc/append x y)
loading …
Set operations
(def x (tc/dataset {:V1 [1 2 2 3 3]}))
(def y (tc/dataset {:V1 [2 2 3 4 4]}))
x
loading …
y
loading …

Intersection

(tc/intersect x y)
loading …

Difference

(tc/difference x y)
loading …

Union

(tc/union x y)
loading …
(tc/concat x y)
loading …

Equality not implemented